【问题标题】:Disable beam touch mode禁用光束触摸模式
【发布时间】:2012-05-10 14:10:30
【问题描述】:

我有一个应用程序,其中一个特殊的 Activity A 能够传输数据:

当Device1在Activity A并且你和Device2配对时(不管Device2在哪里,即使app没有启动),beam touch后数据传输成功。 Activity 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 桌面上并配对设备,您也不会获得光束对话框。你只是得到一个小的振动。这就是我想要的。有可能吗?

提前致谢!

【问题讨论】:

    标签: android nfc android-beam


    【解决方案1】:

    在 Activity 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
      }
    }
    

    您可以通过仅过滤PendingIntent 中的Ndef 技术和/或检查onNewIntent() 中的Tag 对象支持的其他技术来使这一点更加具体。 Android Beam Intent 始终具有Ndef 技术,没有其他技术。

    【讨论】:

    • 在类似的情况下,我注意到我的 onNewIntent() 被 ACTION_NDEF_DISCOVERED 调用。如果我的目标是抑制接收活动的(重新)启动,我是否也应该在 intent.getAction() == ACTION_NDEF_DISCOVERED 时立即返回?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多