接口说明
| 
 描述  | 
 通知名  | 
 附加参数  | 
|---|---|---|
| 
 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);
}
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();
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.error(TAG, “doSubscribe occur exception: %{public}s” ,e.toString());
}
注册并获取NFC场强消息
- 构建消息通知接收者NfcFieldOnAndOffEventSubscriber。
 - 注册NFC场强消息。
 
NfcFieldOnAndOffEventSubscriber接收并处理NFC场强消息。
// 构建消息接收者/注册者
class NfcFieldOnAndOffEventSubscriber extends CommonEventSubscriber {
NfcFieldOnAndOffEventSubscriber (CommonEventSubscribeInfo info) {
super(info);
}
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.info(TAG, “Pure FIELD_ON_DETECTED”);
} else {
HiLog.info(TAG, “Transaction FIELD_ON_DETECTED”);
Intent transactionIntent = (Intent) params.getParam(“transactionIntent”);
}
} else if (NfcController.FIELD_OFF_DETECTED.equals(commonEventData.getIntent().getAction())) {
HiLog.info(TAG, “FIELD_OFF_DETECTED”);
}
HiLog.info(TAG, “NfcFieldOnAndOffEventSubscriber onReceiveEvent: %{public}s”, commonEventData.getIntent().getAction());
}
}
// 注册消息
MatchingSkills matchingSkills = new MatchingSkills();
// 增加获取NFC状态改变消息
matchingSkills.addEvent(NfcController.FIELD_ON_DETECTED);
matchingSkills.addEvent(NfcController.FIELD_OFF_DETECTED);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
HiLog.info(TAG, “subscribeInfo permission: %{public}s”, subscribeInfo.getPermission());
NfcFieldOnAndOffEventSubscriber subscriber = new NfcFieldOnAndOffEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.error(TAG, “doSubscribe occur exception: %{public}s”, e.toString());
}