"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.");}
1
2
3
4
5
6
7
8
9
10
11
12
② 发布带权限的公共事件
构造 CommonEventPublishInfo 对象,设置订阅者的权限。
订阅者在 config.json 中申请所需的权限:
"reqPermissions":[{"name":"com.example.MyApplication.permission","reason":"Obtain the required permission","usedScene":{"ability":[".MainAbility"],"when":"inuse"}},{...}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
发布带权限的公共事件示例代码如下:
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"}},{...}]}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
发布粘性公共事件:
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.");}
1
2
3
4
5
6
7
8
9
10
11
如果订阅拥有指定权限应用发布的公共事件,发布者需要在 config.json 中申请权限:
"reqPermissions":[{"name":"ohos.abilitydemo.permission.PROVIDER","reason":"Obtain the required permission","usedScene":{"ability":["com.hmi.ivi.systemsetting.MainAbility"],"when":"inuse"}}]
1
2
3
4
5
6
7
8
9
10
如果订阅的公共事件是有序的,可以调用 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.");}
EventRunner runner = EventRunner.create();// EventRunner创建新线程,将耗时的操作放到新的线程上执行
MyEventHandler myHandler = new MyEventHandler(runner);// MyEventHandler为EventHandler的派生类,在不同线程间分发和处理事件和Runnable任务@Override
public voidonReceiveEvent(CommonEventData commonEventData){
final AsyncCommonEventResult result =goAsyncCommonEvent();
Runnable task = new Runnable(){@Override
public voidrun(){........// 待执行的操作,由开发者定义
result.finishCommonEvent();// 调用finish结束异步操作}};
myHandler.postTask(task);}
评论(0)