codecamp

鸿蒙OS NFC消息通知

场景介绍

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 状态改变消息

  1. 构建消息通知接收者 NfcStateEventSubscriber。

  1. 注册 NFC 状态改变消息。

  1. 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 场强消息

  1. 构建消息通知接收者NfcFieldOnAndOffEventSubscriber。

  1. 注册 NFC 场强消息。

  1. 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());
   }
鸿蒙OS 卡模拟功能
鸿蒙OS 蓝牙概述
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }