禁用光束触摸模式

时间:2012-05-10 14:10:30

标签: 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>

on做了必要的推动。

但是当我在另一个活动B中时,这也会产生

  1. 其他设备启动应用
  2. 如果应用程序已启动,则会在两台设备上显示触摸模式。我不希望任何设备有改变现在做梁。如果您在Android桌面上并且配对了设备,那么您也不会获得光束对话框。你只是得到一个小振动。多数民众赞成我想要的东西。有可能吗?
  3. 提前致谢!

1 个答案:

答案 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技术,而不是其他技术。