我有一个在线程A和另一个线程B上运行的intent服务,我从intent服务执行以执行某些任务,一旦线程B完成工作,我想通过处理程序将结果发送回线程A.这是我的代码:
@Override
protected void onHandleIntent(Intent intent) {
Log.d("service", "service is started!");
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.arg1==1){
}
}
};
BackgroundWorker backgroundWorker=new BackgroundWorker(handler);
Thread thread=new Thread(backgroundWorker);
thread.start();
}
这是线程B类:
class BackgroundWorker implements Runnable{
private Handler handler;
public BackgroundWorker(Handler handler){
this.handler=handler;
}
@Override
public void run() {
Looper.prepare();
Message msg = Message.obtain();
msg.arg1 = 1;
handler.sendMessage(msg);
Looper.loop();
}
}
所以我的问题是我在backgroundWorker类中发送消息但是它从未在handleMessage中收到。