I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. I can also send commands by pressing buttons.
My problem is whenever the terminal itself is highlighted the UI does not update, as in the commands sent with the buttons, or data received from the serial device. I can type in the terminal itself and the characters come up. If the editText is selected everything updates fine. So if I select the terminal and send a command, it will only be displayed to the screen once I select the editText. A lot of the time after I select the editText it still does not update until I press the terminal again. Very strange. So my invalidate is not working well? Or is it this notifyUpdate method that I am using and don’t really understand?
What I currently do is call a method when data is received, this method calls a handler which updates the screen every 1000ms. This however does not work when the terminal is selected, why is this? I am getting the error: “Only the original thread that created a view hierarchy can touch its views.”
Screenshot: https://i.stack.imgur.com/1m26B.png
//terminal view
EmulatorView view = (EmulatorView) findViewById(R.id.emulatorView);
mEmulatorView = view;
public void onDataReceived(int id, byte[] data)
{
dataReceived = new String(data);
((MyBAIsWrapper) bis).renew(data);
mSession.write(dataReceived);
mSession.notifyUpdate();
viewHandler.post(updateView);
}
Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
@Override
public void run() {
mEmulatorView.invalidate();
viewHandler.postDelayed(updateView, 1000);
}
}
};
Relevant code from library I’m using:
[code]protected void notifyUpdate() {
if (mNotify != null) {
mNotify.onUpdate();
}
}[/code]
and
/**
* Generic callback to be invoked to notify of updates.
*/
public interface UpdateCallback {
/**
* Callback function to be invoked when an update happens.
*/
void onUpdate();
}
This is the description of the method notifyUpdate() – Method in class jackpal.androidterm.emulatorview.TermSession Notify the UpdateCallback registered by setUpdateCallback that the screen has changed.
I thought it might be mSession.notifyUpdate(); in my code, but apparently if I comment that out the same error occurs, that line seems to be doing nothing for me anyway. I’m not sure where the error is to fix it so that my screen is always being updated no matter where I am pressing.
Got it working, I just had to run that write line in a UI thread, even thought I though it was?!