此代码工作正常并更新我的“TextView”并显示“Toast”,这是我的头痛,因为我试图传递 A Runnable obj 而不包括我的 UiHandler,因为它假设是更新我的 UI 线程的桥梁但是我的活动得到了更新,没有一个错误????? 这不应该是因为 CustomHandlerThread 应该是一个不同的线程
为什么会这样?
我的活动
公共类 TestActivity 扩展 BaseActivity {
Runnable task;
@BindView(R.id.send_test_message)
Button send_test_message;
private Handler mUiHandler = new Handler();
private MyWorkerThread mWorkerThread;
@Override
public void initViews() {
}
@Override
public void attachViewsListeners() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
ButterKnife.bind(this);
task = new Runnable() { ///this is normally work I have no problem with
///that
@Override
public void run() {
mUiHandler.post(new Runnable() {
@Override
public void run() {
send_test_message.setText("Change--->1");
}
});
}
};
}
@Override
protected void onStart() {
super.onStart();
mWorkerThread = new MyWorkerThread("myWorkerThread");
mWorkerThread.start();
mWorkerThread.prepareHandler();
mWorkerThread.postTask(task);
mWorkerThread.postTask(new Runnable() { /// why this task work with no
///error ?
@Override
public void run() {
send_test_message.setText("Change--->2");
}
});
}
@Override
protected void onDestroy() {
mWorkerThread.quit();
super.onDestroy();
}
我的处理线程
//MyWorkerThread.java
public class MyWorkerThread extends HandlerThread {
Handler mWorkerHandler;
public MyWorkerThread(String name) {
super(name);
}
public void postTask(Runnable task) {
mWorkerHandler.post(task);
}
public void prepareHandler() {
mWorkerHandler = new Handler(getLooper());
Log.e("MyWorkerThread--->",Looper.myLooper().getThread().getName()); //-->main
Log.e("MyWorkerThread--->",getLooper().getThread().getName());//-->//myWorkerThread
Log.e("MyWorkerThread--->",Thread.currentThread().getName()); //-->main
}
}
我按照这个链接作为参考
是我的线程在 MainThread 上运行还是 getLooper() 用 MainThread Looper 初始化我的 HandlerThread,这些日志消息是真的吗