阅读(1782)
赞(9)
鸿蒙OS NFC消息通知
2020-09-17 09:48:34 更新
场景介绍
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 | - |
注册并获取 NFC 状态改变消息
- 构建消息通知接收者 NfcStateEventSubscriber。
- 注册 NFC 状态改变消息。
- NfcStateEventSubscriber 接收并处理 NFC 状态改变消息。
// 构建消息接收者/注册者
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());
}
注册并获取 NFC 场强消息
- 构建消息通知接收者NfcFieldOnAndOffEventSubscriber。
- 注册 NFC 场强消息。
- NfcFieldOnAndOffEventSubscriber 接收并处理 NFC 场强消息。
// 构建消息接收者/注册者
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());
}