android在某些情况下无法写入屏幕

时间:2012-12-20 11:07:35

标签: java android session terminal screen

我正在尝试使用android中的库连接到终端模拟器,这将连接到串行设备,并应显示发送/接收的数据。我应该能够通过终端下方的文本框通过连接发送数据,或者通过键入终端本身并在两种情况下按键盘输入。库中有一个名为“write”的函数可以写入仿真器屏幕。然而,有时它会起作用,有时却不起作用。

在我的代码中标记为[1],[2]和[3]的行中,它可以正常工作,[4]和[5]则没有。谁能明白为什么?我在4和5之前创建终端会话,所以它应该适用于它们,它不适用。然而,当我开始为1,2,3调用时,它工作正常吗?!

public class TermActivity extends Activity
{
private EditText mEntry;
private EmulatorView mEmulatorView;
private TermSession mSession;
private InputStream bis;
private OutputStream bos;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.term_activity);

    /* Text entry box at the bottom of the activity.  Note that you can
       also send input (whether from a hardware device or soft keyboard)
       directly to the EmulatorView. */
    mEntry = (EditText) findViewById(R.id.term_entry);
    mEntry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int action, KeyEvent ev) {
            // Ignore enter-key-up events
            if (ev != null && ev.getAction() == KeyEvent.ACTION_UP) {
                return false;
            }
            // Don't try to send something if we're not connected yet
            TermSession session = mSession;
            if (mSession == null) {
                return true;
            }

            Editable e = (Editable) v.getText();
            // Write to the terminal session
            //for when i press enter on keyboard.
            [1] session.write(e.toString());
            [2] session.write("test");
            [3] session.write('\r');
            TextKeyListener.clear(e);
            return true;
        }
    });



    /**
     * EmulatorView setup.
     */
    EmulatorView view = (EmulatorView) findViewById(R.id.emulatorView);
    mEmulatorView = view;

    /* Let the EmulatorView know the screen's density. */
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    view.setDensity(metrics);

    /* Create a TermSession. */
    Intent myIntent = getIntent();
    String sessionType = myIntent.getStringExtra("type");
    TermSession session;

    if (sessionType != null && sessionType.equals("telnet")) {
        /* Telnet connection: we need to do the network connect on a
           separate thread, so kick that off and wait for it to finish. */
      //  connectToTelnet(myIntent.getStringExtra("host"));

         byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
         byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
        bis = new ByteArrayInputStream(b);
        bos = new ByteArrayOutputStream();


         session = new TelnetSession(bis, bos);


         mEmulatorView.attachSession(session);
         [4]session.write("test");
         mSession = session;
         [5]session.write("test");


        return;
    } else {
        // Create a local shell session.
        session = createLocalTermSession();
        mSession = session;
    }

    /* Attach the TermSession to the EmulatorView. */
    view.attachSession(session);

    /* That's all you have to do!  The EmulatorView will call the attached
       TermSession's initializeEmulator() automatically, once it can
       calculate the appropriate screen size for the terminal emulator. */
}

Socket mSocket;
private static final int MSG_CONNECTED = 1;

/* Create the TermSession which will handle the Telnet protocol and
   terminal emulation. */
private void createTelnetSession() {
    Socket socket = mSocket;

    // Get the socket's input and output streams
    InputStream termIn;
    OutputStream termOut;
    try {
       termIn = socket.getInputStream();
       termOut = socket.getOutputStream();
    } catch (IOException e) {
        //Handle exception here
        return;
    }

    /* Create the TermSession and attach it to the view.  See the
       TelnetSession class for details. */
    byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
    byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
    bis = new ByteArrayInputStream(b);
    bos = new ByteArrayOutputStream();


TermSession session = new TelnetSession(bis, bos);  
    mEmulatorView.attachSession(session);
    mSession = session;
    session.write("test");
    try {
        bos.write(a);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:1)

终端仿真器需要几秒钟来初始化连接并启动。在此之前,写入模拟器的字符串将被静默删除。

所以,你可以:

  • 如果不需要立即写入模拟器,则忽略此项
  • 等待一段固定的时间
  • 检查终端仿真器api以查看是否有api来询问连接状态