无法调用NFC onNewIntent()方法

问题描述 投票:0回答:1

我正在尝试使用前台调度读取Mifare Classic 1k卡的ID。正如我从日志中看到的,我可以启用前台调度,但不能调用onNewIntent()方法。任何建议,将不胜感激。

main activity.Java

...
@Override
   protected void onResume() {
      setupForegroundDispatch(this, mAdapter);
      super.onResume();
} 

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    System.out.println("Setup FGD.");  // i can see this output.

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Notice that this is the same filter as in our manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    System.out.println("Enabled FGD.");  // and this one.
}

protected void onNewIntent(Intent intent) { 
    System.out.println("Intent."); but i cannot see this one,
    handleIntent(intent);
}

private void handleIntent(Intent intent) {      
    System.out.println("Handle.");  // and this one.
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        System.out.println("NDEF discovered.");
         ....

AndroidManifest

....
 <activity
        android:name=".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.TECH_DISCOVERED" />
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
         </intent-filter>

        <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
    </activity>
....
android android-intent nfc intentfilter mifare
1个回答
4
投票

如果您希望能够检测到每个MIFARE Classic标记(而不仅仅是那些包含文本记录的标记(或MIME类型为text / plain的MIME类型记录),则应调整前台调度以检测特定标记技术而不是特定的NDEF记录类型:

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter[] filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
    String[][] techList = new String[][] { new String[] { MifareClassic.class.getName() } };

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

如果您还希望在具有Broadcom NFC芯片组的设备上获得MIFARE Classic标签的UID(由于恩智浦专有技术的许可问题,这些设备无法将MIFARE Classic检测为MIFARE Classic),您可以替换所有NfcA标签(MIFARE) Classic将在所有设备上被检测为NfcA,因此您不需要同时过滤NfcAMifareClassic):

    String[][] techList = new String[][] { new String[] { NfcA.class.getName() } };

最后,清单中的intent过滤器与代码中的intent过滤器不匹配!您的前台调度的等效意图过滤器将是:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

我在上面显示的前台调度的清单等价物是:

<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
           android:resource="@xml/nfc_tech_filter" />

使用nfc_tech_filter.xml包含:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
</resources>

或者(如果匹配NfcA):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
</resources>
© www.soinside.com 2019 - 2024. All rights reserved.