"reqPermissions":[{"name":"ohos.permission.COMMONEVENT_STICKY","reason":"Obtain the required permission","usedScene":{"ability":[".MainAbility"],"when":"inuse"}},{...}]
try {Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder().withAction("com.my.test").build();intent.setOperation(operation);CommonEventData eventData = new CommonEventData(intent);CommonEventManager.publishCommonEvent(eventData); HiLog.info(LABEL_LOG,"Publish succeeded");}catch(RemoteException e){HiLog.error(LABEL_LOG,"Exception occurred during publishCommonEvent invocation.");}
② 發布帶權限的公共事件
構造 CommonEventPublishInfo 對象,設置訂閱者的權限。
訂閱者在 config.json 中申請所需的權限:
"reqPermissions":[{"name":"com.example.MyApplication.permission","reason":"Obtain the required permission","usedScene":{"ability":[".MainAbility"],"when":"inuse"}},{...}]
發布帶權限的公共事件示例代碼如下:
Intent intent = new Intent();Operation operation = new Intent.OperationBuilder().withAction("com.my.test").build();intent.setOperation(operation);CommonEventData eventData = new CommonEventData(intent);CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();String[] permissions ={"com.example.MyApplication.permission"};publishInfo.setSubscriberPermissions(permissions);// 設置權限try { CommonEventManager.publishCommonEvent(eventData, publishInfo); HiLog.info(LABEL_LOG,"Publish succeeded");}catch(RemoteException e){HiLog.error(LABEL_LOG,"Exception occurred during publishCommonEvent invocation.");}
{"reqPermissions":[{"name":"ohos.permission.COMMONEVENT_STICKY","reason":"Obtain the required permission","usedScene":{"ability":[".MainAbility"],"when":"inuse"}},{...}]}
發布粘性公共事件:
CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();publishInfo.setSticky(true);// 設置屬性為粘性公共事件try { CommonEventManager.publishCommonEvent(eventData, publishInfo);}catch(RemoteException e){HiLog.error(LABEL,"Exception occurred during publishCommonEvent invocation.");}
class MyCommonEventSubscriber extends CommonEventSubscriber {MyCommonEventSubscriber(CommonEventSubscribeInfo info){super(info);}@Override public voidonReceiveEvent(CommonEventData commonEventData){}}
String event ="com.my.test";MatchingSkills matchingSkills = new MatchingSkills();matchingSkills.addEvent(event);// 自定義事件matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_SCREEN_ON);// 亮屏事件CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);MyCommonEventSubscriber subscriber = new MyCommonEventSubscriber(subscribeInfo);try {CommonEventManager.subscribeCommonEvent(subscriber);}catch(RemoteException e){HiLog.error(LABEL,"Exception occurred during subscribeCommonEvent invocation.");}
如果訂閱擁有指定權限應用發布的公共事件,發布者需要在 config.json 中申請權限:
"reqPermissions":[{"name":"ohos.abilitydemo.permission.PROVIDER","reason":"Obtain the required permission","usedScene":{"ability":["com.hmi.ivi.systemsetting.MainAbility"],"when":"inuse"}}]
如果訂閱的公共事件是有序的,可以調用 setPriority() 指定優先級:
String event ="com.my.test";MatchingSkills matchingSkills = new MatchingSkills();matchingSkills.addEvent(event);// 自定義事件CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);subscribeInfo.setPriority(100);// 設置優先級,優先級取值范圍[-1000,1000],值默認為0。MyCommonEventSubscriber subscriber = new MyCommonEventSubscriber(subscribeInfo);try {CommonEventManager.subscribeCommonEvent(subscriber);}catch(RemoteException e){HiLog.error(LABEL,"Exception occurred during subscribeCommonEvent invocation.");}