//创建主线程的handler
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
Message message = new Message();
//向子线程发消息
threadHandler .sendMessDelayed(message , 1000);
}
};
private Handler threadHandler;
onCreate()方法中:
HandlerThread thread = new HandlerThread("handlerThread"); //HandlerThread解决线程并发问题,可以查看源码。通过它可以获得Looper(Looper不会为空)
thread .start();
//创建子线程的handler
threadHandler = new Handler(thread.getLooper()){
public void handleMessage(Message msg) {
Message message = new Message();
//向主线程发消息
handler.sendMessDelayed(message , 1000);
}
};
时间: 2024-10-18 23:29:45