NFC 消息通知(Notification)是 HarmonyOS 内部或者与应用之间跨进程通讯的机制,注册者在注册消息通知后,一旦符合条件的消息被发出,注册者即可接收到该消息。
描述 | 通知名 | 附加参数 |
---|---|---|
NFC状态 | usual.event.nfc.action.ADAPTER_STATE_CHANGED | extra_nfc_state |
进场消息 | usual.event.nfc.action.RF_FIELD_ON_DETECTED | extra_nfc_transaction |
离场消息 | usual.event.nfc.action.RF_FIELD_OFF_DETECTED | - |
// 构建消息接收者/注册者
class NfcStateEventSubscriber extends CommonEventSubscriber {
NfcStateEventSubscriber (CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
if (commonEventData == null || commonEventData.getIntent() == null) {
return;
}
if (NfcController.STATE_CHANGED.equals(commonEventData.getIntent().getAction())) {
IntentParams params = commonEventData.getIntent().getParams();
if (params != null) {
int currState = commonEventData.getIntent().getIntParam(NfcController.EXTRA_NFC_STATE, NfcController.STATE_OFF);
}
}
}
}
// 注册消息
MatchingSkills matchingSkills = new MatchingSkills();
// 增加获取 NFC 状态改变消息
matchingSkills.addEvent(NfcController.STATE_CHANGED);
matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_NFC_ACTION_ADAPTER_STATE_CHANGED);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
NfcStateEventSubscriber subscriber = new NfcStateEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.e(TAG, "doSubscribe occur exception:" + e.toString());
}
// 构建消息接收者/注册者
class NfcFieldOnAndOffEventSubscriber extends CommonEventSubscriber {
NfcFieldOnAndOffEventSubscriber (CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
if (commonEventData == null || commonEventData.getIntent() == null) {
return;
}
if (NfcController.FIELD_ON_DETECTED.equals(commonEventData.getIntent().getAction())) {
IntentParams params = commonEventData.getIntent().getParams();
if (params == null) {
HiLog.i(TAG, "Pure FIELD_ON_DETECTED");
} else {
HiLog.i(TAG, "Transaction FIELD_ON_DETECTED");
Intent transactionIntent = (Intent) params.getParam("transactionIntent");
}
} else if (NfcController.FIELD_OFF_DETECTED.equals(commonEventData.getIntent().getAction())) {
HiLog.i(TAG, "FIELD_OFF_DETECTED");
}
HiLog.i(TAG, "MyFieldOnAndOffEventSubscriber onReceiveEvent ....:" + commonEventData.getIntent().getAction());
}
}
// 注册消息
MatchingSkills matchingSkills = new MatchingSkills();
// 增加获取 NFC 状态改变消息
matchingSkills.addEvent(NfcController.FIELD_ON_DETECTED);
matchingSkills.addEvent(NfcController.FIELD_OFF_DETECTED);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(DomainMode.BOTH, matchingSkills);
HiLog.i(TAG, "subscribeInfo permission: " + subscribeInfo.getPermission());
MyFieldOnAndOffEventSubscriber subscriber = new MyFieldOnAndOffEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.e(TAG, "doSubscribe occur exception:" + e.toString());
}
备案信息: 粤ICP备15087711号-2
Copyright © 2008-2024 啊嘎哇在线工具箱 All Rights.
本站所有资料来源于网络,版权归原作者所有,仅作学习交流使用,如不慎侵犯了您的权利,请联系我们。