Reputation: 5974
I am getting a wrong thread error, so I am thinking I need to be running invalidate from the UI thread. Usually I have something like:
public void run() {
runOnUiThread(new Runnable() {
}});
But I need to name this runnable to reference it from a method. How do i incorporate runOnUiThread into this?
Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
@Override
public void run() {
mEmulatorView.invalidate();
if (statusBool == true) {
for (int i = 1; i < dataReceived.length() - 1; i++) {
if (dataReceived.charAt(i) == '>') {
Log.d(TAG, "found >");
deviceStatus = 0;
}
if (dataReceived.charAt(i) == '#'
&& dataReceived.charAt(i - 1) != ')') {
Log.d(TAG, "found #");
deviceStatus = 1;
}
if ((i + 1) <= (dataReceived.length())
&& dataReceived.charAt(i) == ')'
&& dataReceived.charAt(i + 1) == '#') {
Log.d(TAG, "found config )#");
deviceStatus = 2;
}
}
statusBool = false;
viewHandler.postDelayed(updateView, 1000);
}
}
};
calling it:
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
((MyBAIsWrapper) bis).renew(data);
mSession.write(dataReceived);
viewHandler.post(updateView);
}
Upvotes: 0
Views: 211
Reputation: 15434
You don't need to name it. If you want to post itself you can just use this
keyword:
viewHandler.postDelayed(this, 1000);
UPDATE
Wrong thread error caused by mSession.write(dataReceived);
. updateView
doesn't cause any problems. Try to wrap mSession.write
to runnable and call it on ui thread.
Upvotes: 1
Reputation: 5974
Got it working, I just had to run that write line in a UI thread, even though I thought it was?!
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
@Override
public void run() {
mSession.write(dataReceived);
}});
viewHandler.post(updateView);
}
Upvotes: 1