在开发过程中service想要控制多个ability时,可以考虑使用公共事件处理。
发布无序的公共事件:
//发布公共事件 同步修改卡片与页面
public void subscribeEvent(String id,String status) {
try {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction("play")
.build();
intent.setOperation(operation);
intent.setParam("id",id);
intent.setParam("status",status);
CommonEventData eventData = new CommonEventData(intent);
CommonEventManager.publishCommonEvent(eventData);
HiLog.info(TAG, "Publish succeeded");
} catch (RemoteException e) {
HiLog.error(TAG, "Exception occurred during publishCommonEvent invocation.");
}
}
订阅该公共事件:
class MyCommonEventSubscriber extends CommonEventSubscriber {
MyCommonEventSubscriber(CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
HiLog.info(TAG,"订阅Seccess");
id= Integer.parseInt(commonEventData.getIntent().getStringParam("id"));
status=commonEventData.getIntent().getStringParam("status");
HiLog.info(TAG,"订阅数据状态="+status);
HiLog.info(TAG,"订阅数据歌曲id="+id);
}
}
本文根据官方文档学习体验整理