我收到了错误的线程错误,所以我想我需要从 UI 线程运行无效。通常我有类似的东西:
public void run() {
runOnUiThread(new Runnable() {
}});
但是我需要命名这个 runnable 以从方法中引用它。我如何将 runOnUiThread 纳入其中?
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);
}
}
};
调用它:
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
((MyBAIsWrapper) bis).renew(data);
mSession.write(dataReceived);
viewHandler.post(updateView);
}