onNewIntent() 上的 NFC 标签为空

Stu*_*yte 2 android nfc

我一直在开发一个应用程序,它使用 NFC 标签来发挥一些魔力。

一切都很好,直到最近我更改了一些与之前有效的 NFC 代码无关的代码。

当我通过 NFC 点击启动我的应用程序时,一切正常,当我在应用程序运行时点击时,我将在 onNewIntent() 中收到未来的 NFCTag。

当我通过图标启动应用程序并尝试在应用程序运行时点击时,我的 onNewIntent() 方法会被调用,但是当我尝试从意图中获取 NFCTag 作为额外内容时,它会返回 null。

我是否正确地认为,即使它为空,自从我的 onNewIntent() 被调用以来我已经正确设置了 ForegroundDispatch ?

这是代码...

protected void onResume() {
    if(this.mNfcAdapter==null) {
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter nfcFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        nfcFilter.addDataType("application/application.myorg.myapp");
    } catch (MalformedMimeTypeException e) {
        Log.e(TAG, "Error Setting FD for NFC", e);
    }
    String[][] mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] {nfcFilter}, mTechLists);
}

protected void onPause() {
    super.onPause();
    mNfcAdapter.disableForegroundDispatch(this);
    Log.d(TAG, "Activity is pausing");
}

protected void onNewIntent(Intent intent) {
    Log.d(TAG, "NFC TAP WHILE ACTIVE");
    Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag!=null) {
       //NEVER CALLED WHEN LAUNCHED VIA ICON (NOT NFC)
       Log.d(TAG, "TAG IS NOT NULL");
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 IntentFilter 中设置的 MIME 类型与我在 Manifest 中设置的相同。

编辑

我的清单

<activity
   android:name="org.mypackage.myapp.MainActivity"
   android:label="@string/app_name" >
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />

          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
     <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />                                       
          <data android:mimeType="application/org.mypackage.myapp" />
          <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

我的标签是什么样的

+---------------------------------------------------+
| MIME:application/org.mypackage.myapp | StringData |
+---------------------------------------------------+
| EXT:android:com:pkg | org.mypackage.myapp         |
+---------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

Mic*_*and 5

问题在于如何在方法中检索意图onNewIntent()。目前,您正在使用getIntent()来获取尝试检索EXTRA_TAG. 除非您用来setIntent(...)更改它(显然您在代码的相关部分中没有这样做),否则这将返回最初启动您的活动的意图。NFC 发现意图onNewIntent()通过参数传递给方法Intent intent。所以使用这个应该可以解决问题:

protected void onNewIntent(Intent intent) {
    Log.d(TAG, "NFC TAP WHILE ACTIVE");
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if (tag != null) {
       Log.d(TAG, "TAG IS NOT NULL");
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您可能想检查您收到的意图是否确实具有您期望的意图操作(例如ACTION_NDEF_DISCOVERED)。


另一件事:您可以在注册意图时安全地设置mTechLists为。技术列表仅用于意图过滤器。nullNDEF_DISCOVEREDTECH_DISCOVERED