使用GlobalEvent可以让程序的两个entry产生即时的互动:
1)比如setting程序里面,用户修改了程序配置以后,发GlobalEvent出去,让background程序重新初始化。
2)background程序定时发GlobalEvent给GUI程序,检测GUI程序的状态。如果GUI程序没有对GlobalEvent做出回应,说明该程序死掉了,background程序就再次启动GUI程序。
比较:
和用共享数据(RuntimeStore/PersistantStore/数据库/配置文件)的方式相比,程序简化,不需要定时轮询,节省CPU使用。
代码片段===================================================================
发消息出去:
ApplicationManager.getApplicationManager().postGlobalEvent(Util.GLOBAL_ID_EXIT);
收听消息:
class MyApplication extends UiApplicationimplements GlobalEventListener{
...
addGlobalEventListener(this); //我要听
...
public void eventOccurred(long guid, int data0,int data1, Object object0,
Object object1) {
if (guid == Util.GLOBAL_ID_EXIT) { //我听到了
deregisterNDEFReaderListener();
System.exit(0);
}
}
参考:
http://blog.csdn.net/kunming1987/article/details/7620121