sof*_*ply 1 android nfc android-beam
我有一个应用程序,其中一个特殊的活动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>
Run Code Online (Sandbox Code Playgroud)
在做必要的推动.
但是当我在另一个活动B中时,这也会产生
提前致谢!
在活动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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过仅过滤Ndef
技术PendingIntent
和/或检查对象支持的onNewIntent()
其他技术来使其更具体一些Tag
.Android Beam意图总是有Ndef
技术,没有其他技术.
归档时间: |
|
查看次数: |
3445 次 |
最近记录: |