W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
NFC 消息通知(Notification)是 HarmonyOS 內部或者與應用之間跨進程通訊的機制,注冊者在注冊消息通知后,一旦符合條件的消息被發(fā)出,注冊者即可接收到該消息。
描述 | 通知名 | 附加參數 |
---|---|---|
NFC狀態(tài) | 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 狀態(tài)改變消息
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 狀態(tài)改變消息
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());
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: