No Output with Datainputstream

Asked

Viewed 36 times

0

I am trying to build an application that works as a terminal emulator, but simply no output is generated.

Here’s my code, it might be useful:

package me.craftygaming.TermDroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import me.craftygaming.TermDroid.R;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * Created by ********* on 11/01/17.
 */
public class ActivityTerminal extends Activity {
    TextView console;
    EditText cmd;
    DataOutputStream os;
    DataInputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Activityterminal);
        Intent intent = getIntent();
        String message = intent.getStringExtra("me.craftygaming.TDroid.MODE");
        String args = intent.getStringExtra("me.craftygaming.TDroid.ARGS");
        File f = new File(message);
        File term = new File(f, "system/bin/python");

        System.out.println(f.getAbsolutePath());
        console = (TextView)findViewById(R.id.console);
        cmd = (EditText)findViewById(R.id.command);

        try {
            Process p = Runtime.getRuntime().exec("/system/bin/sh -");
            os = new DataOutputStream(p.getOutputStream());
            is = new DataInputStream(p.getInputStream());

            os.writeBytes(term.getCanonicalPath()+"\n");
            os.writeBytes("echo \"Hi\"\n");
        }catch(IOException ex){}
        updater.start();

    }
    public void Submit(View view){
        try{
      String comand = cmd.getText().toString();
        os.writeBytes(comand);
            console.append("\n>> "+comand+"\n");
            cmd.setText("");
            } catch (IOException e){

                CharSequence text = "Error! "+e.getLocalizedMessage();
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(getApplicationContext(), text, duration);
                toast.show();
            }
    }
    Thread updater = new Thread() {
        @Override
        public void run() {
            while(true){
                try {
                    while (is.available() == 0) {
                    }
                    console.append(is.readUTF() + "\n");
                }catch(IOException e){
                    console.append("ERROR READING!");
                }
            }

        }
    };
}

Also, is there any better way to get the data from DataInputStream?

  • In the code in question you are not reading anything from InputStream. The idea is that after executing the command your application has some kind of loop to read the results for a String or StringBuilder and then write the result in TextView. At a glance in that Soen Example.

  • Note that building a terminal emulator from scratch is not usually a trivial task. Maybe it’s worth giving a read on some open source project like the Terminal Emulator for Android.

  • Oops, I was going to post on Stackoverflow in English, but I ended up posting on in English :D. @Anthonyaccioly Loop is on Thread Updater.

  • This is strange Gabrieltk, I understood your attempt to read the return of command on another Thread, but this whole construction, from the way you are trying to get the parallelism to the use of a while inside the other to "wait" for the exit does not make much sense and is subject to mistakes. Again, I don’t want to discourage you if this is a personal exercise, but if the idea is to create a terminal emulator for real I would tell you to read the emulator code above or something with the Qpython.

  • @Anthonyaccioly is more of a personal exercise. But I don’t understand... What is the right way to read the output and send it to Textview?? Sorry, I’m more of a C#.

  • Hi Gabriel, follow a hack how to execute and get the return of commands in the bash: Reply on the Soen. Note that the solution is very limited, without parallelism, abusing Ctrl+Z, etc.

  • As for playing in a textView, append will work until you begin to realize that it is necessary to limit the size of the buffer, emulate escape sequences, resize the screen, etc. In practice, for real problems you will need a custom view.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.