公共事件类似于Android的广播,有可能就是广播,下面将提到一个疑点。
一个公共事件就两个使用地方,一个是发布事件,一个是接收事件,所以这里写两个Ability,MainAbility接收事件,SecondAbility发送事件。
首先定义一个EventHandler类,用来异步处理消息:
public class MyEventHandler extends EventHandler {
public MyEventHandler(EventRunner runner) throws IllegalArgumentException {
super(runner);
}
}
然后再MainAbility中订阅事件:
String action = "com.example.eventsubscriberdemo";
MainEventSubscriber subscriber;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
InsideListener listener = new InsideListener();
findComponentById(ResourceTable.Id_btn_goto_publish).setClickedListener(listener);
//订阅无序的公共事件
MatchingSkills matchingSkills = new MatchingSkills();
matchingSkills.addEvent(action); // 自定义事件
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
subscriber = new MainEventSubscriber(subscribeInfo);
HiLog.info(LABEL_LOG, "onStart() subscribeCommonEvent");
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "subscribeCommonEvent occur exception.");
}
}
EventRunner runner = EventRunner.create(); //EventRunner 创建新线程,将耗时的操作放到新的线程上执行
MyEventHandler myHandler = new MyEventHandler(runner); //MyEventHandler为EventHandler的派生类,在不同线程间分发和处理事件和Runnabl
class MainEventSubscriber extends CommonEventSubscriber {
MainEventSubscriber(CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
final AsyncCommonEventResult result = goAsyncCommonEvent();
Runnable task = new Runnable() {
@Override
public void run() {
String param=commonEventData.getIntent().getStringParam("param");
HiLog.info(LABEL_LOG, "主頁onReceiveEvent() param="+param);
HiLog.info(LABEL_LOG, "主頁onReceiveEvent() code="+commonEventData.getCode());
HiLog.info(LABEL_LOG, "主頁onReceiveEvent() data="+commonEventData.getData());
result.finishCommonEvent(); // 调用finish结束异步操作
}
};
myHandler.postTask(task);
}
}
在Stop中取消订阅:
@Override
protected void onStop() {
super.onStop();
try {
CommonEventManager.unsubscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "unsubscribeCommonEvent occur exception.");
}
}
然后再SecondAbility中发送事件:
@Override
public void onClick(Component component) {
switch (component.getId()) {
case ResourceTable.Id_btn_push_event:
HiLog.info(LABEL_LOG, "SecondSlice btn_start_remote_fa");
try {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction(action)
.build();
intent.setOperation(operation);
intent.setParam("param","无序事件");
CommonEventData eventData = new CommonEventData(intent);
CommonEventManager.publishCommonEvent(eventData);
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "publishCommonEvent occur exception.");
}
break;
case ResourceTable.Id_btn_push_order_common_event:
//有序的公共事件
CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();
publishInfo.setOrdered(true); // 设置属性为有序公共事件
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction(action)
.build();
intent.setOperation(operation);
intent.setParam("param","有序事件");
CommonEventData eventData = new CommonEventData(intent);
eventData.setCode(10086);
eventData.setData("都给我听好了!");
try {
CommonEventManager.publishCommonEvent(eventData, publishInfo); // 指定resultSubscriber为有序公共事件最后一个接收者。
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "publishCommoneEvent occur exception.");
}
break;
case ResourceTable.Id_btn_push_permissions_common_event:
HiLog.info(LABEL_LOG, "publishCommoneEvent 带权限的公共事件");
//带权限的公共事件
CommonEventPublishInfo publishInfoPermissions = new CommonEventPublishInfo();
String[] permissions = {"com.example.eventsubscriberdemo.permission" };
publishInfoPermissions.setSubscriberPermissions(permissions); // 设置权限
Intent intentPermissions = new Intent();
Operation operationPermissions = new Intent.OperationBuilder()
.withAction(action)
.build();
intentPermissions.setParam("param","权限事件");
intentPermissions.setOperation(operationPermissions);
CommonEventData eventDataPermissions = new CommonEventData(intentPermissions);
try {
CommonEventManager.publishCommonEvent(eventDataPermissions, publishInfoPermissions);
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "publishCommoneEvent occur exception.");
}
break;
case ResourceTable.Id_btn_push_sticky_common_event:
//粘性的公共事件
CommonEventPublishInfo publishInfoSticky = new CommonEventPublishInfo();
publishInfoSticky.setSticky(true); // 设置属性为粘性公共事件
Intent intentSticky = new Intent();
Operation operationSticky = new Intent.OperationBuilder()
.withAction(action)
.build();
intentSticky.setParam("param","粘性事件");
intentSticky.setOperation(operationSticky);
CommonEventData eventDataSticky = new CommonEventData(intentSticky);
try {
CommonEventManager.publishCommonEvent(eventDataSticky, publishInfoSticky); // 指定resultSubscriber为有序公共事件最后一个接收者。
} catch (RemoteException e) {
HiLog.info(LABEL_LOG, "publishCommoneEvent occur exception.");
}
break;
default:
break;
}
}
四个按钮,分别发送了无序公共事件,有序公共事件,带权限的公共事件,还有粘性事件,无序公共事件是可以发送的:
01-03 09:39:43.550 11462-11462/com.example.eventsubscriberdemo I 01100/My_log: SecondSlice btn_start_remote_fa
01-03 09:39:43.555 11462-17589/com.example.eventsubscriberdemo I 01100/My_log: 主頁onReceiveEvent() param=无序事件
01-03 09:39:43.556 11462-17589/com.example.eventsubscriberdemo I 01100/My_log: 主頁onReceiveEvent() code=0
有序公共事件也没有问题,还能传递参数:
01-03 09:41:13.438 11462-17589/com.example.eventsubscriberdemo I 01100/My_log: 主頁onReceiveEvent() param=有序事件
01-03 09:41:13.438 11462-17589/com.example.eventsubscriberdemo I 01100/My_log: 主頁onReceiveEvent() code=10086
01-03 09:41:13.438 11462-17589/com.example.eventsubscriberdemo I 01100/My_log: 主頁onReceiveEvent() data=都给我听好了!
但是带权限的事件,我试了我能想到的一切可能,都没有成功,不知道为什么,等以后知道了再更新,带权限的公共事件,需要在订阅者里配置权限,我这里是MainAbility:
"orientation": "unspecified",
"name": "com.example.eventsubscriberdemo.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "EventSubscriberDemo",
"type": "page",
"launchType": "standard",
"reqPermissions": [
{
"name": "com.example.eventsubscriberdemo.permission",
"reason": "get right",
"usedScene": {
"ability": ["com.example.eventsubscriberdemo.SecondAbility"],
"when": "inuse"
}
}
]
最后是粘性事件,直接报错奔溃,报错信息是:
缺少Android的粘性广播权限,有可能鸿蒙的公共事件,就是Android的广播吧。
代码:https://download.csdn.net/download/y280903468/14014645