1.在全局变量中声明Handler,方便Activity和BroadcaseReceiver操作和传递Handler
private Handler handler;
public Handler getHandler() {
return handler;
}
public void setHandler(Handler handler) {
this.handler = handler;
}
2. 在Activity中声明Handler并实例化,赋值给全局变量中的Handler,并接受BroadcaseReceiver的message返回值
private Handler handlerReceiver;
handlerReceiver = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what==1) {
Intent intent = new Intent(MainActivity.this,MainActivity.class);
startActivity(intent);
}
return false;
}
});
application.setHandler(handlerReceiver);
if(application.getHandler()!=null){
application.getHandler().sendEmptyMessage(1);
}