我有一个应用程序,其中特殊活动A能够传输数据:
当Device1处于活动A并且您将其与Device2配对时(无论Device2在哪里,即使应用程序未启动),数据也会在光束触摸后成功传输。活动A具有意图过滤器:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/de.my.app" />
</intent-filter>
on做了必要的推动。
但是当我在另一个活动B中时,这也会产生
提前致谢!
答案 0 :(得分:5)
在活动B中,您可以启用前台调度,忽略任何NFC意图并禁用发送Android Beam消息:
private NfcAdapter nfcAdapter;
protected void onCreate(Bundle savedInstanceState) {
...
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
// turn off sending Android Beam
nfcAdapter.setNdefPushMessage(null, this);
}
protected void onResume() {
// catch all NFC intents
Intent intent = new Intent(getApplicationContext(), getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
nfcAdapter.enableForegroundDispatch(this, pintent, null, null);
}
protected void onPause() {
nfcAdapter.disableForegroundDispatch(this);
}
protected void onNewIntent(Intent intent) {
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) {
return; // ignore NFC intents
}
}
通过仅对Ndef
中的PendingIntent
技术进行过滤和/或检查onNewIntent()
Tag
对象的其他技术,您可以更加具体一点支持。 Android Beam意图始终采用Ndef
技术,而不是其他技术。