java - NFC - OnNewIntent() 有时无法在 Android 设备上运行

标签 java android nfc

我一直在我的应用程序中检测支持 NFC 的卡以读取标签值。以下是我的代码

OnCreate()

 nfcAdapter = NfcAdapter.getDefaultAdapter(AuthDriverCard.this);
    piTap = PendingIntent.getActivity(
            this, TAP_REQUEST_CODE, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilter = new IntentFilter[]{techDetected, tagDetected, ndefDetected};

恢复时

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

暂停

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

OnNewIntent()

  protected void onNewIntent(Intent intent) {
    // my logic here
   }

这段代码几乎每次都能正常工作。但有时卡没有检测到,我发现OnNewIntent()没有触发

可能是什么问题。我还需要在 list 中设置 Intent 过滤器吗?到目前为止我还没有在 list 中设置它,但它对我来说没有任何问题。我的java代码有问题吗?

请提出建议

注意 - 重新启动(杀死应用程序后)应用程序可以解决问题。之后卡检测就开始工作了。

最佳答案

如果您尚未添加以下内容,则需要在 list 文件的 Intent 过滤器部分中添加以下内容:

<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>

例如:

<application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.LauncherActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>

            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

关于java - NFC - OnNewIntent() 有时无法在 Android 设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421994/

相关文章:

java - 为什么 Lombok @Builder 与此构造函数不兼容?

java - 使用 Java 8 自签名证书

android - Phonegap 网络连接 - 无法读取未定义的属性 'type'

java - int argb color 输出奇怪的值

java - 如何从 BluetoothChat 中的输入/输出流读取/写入原始十六进制字节?

android - NFC 阅读器和手机 : unique authentication without app

Java:在列表中添加或删除元素时避免使用 'instanceof'

android - 使用额外数据从 NFC 标签启动 Android 应用程序

android - 使用 Jelly Bean 的简单安全配对(蓝牙)与 NFC 配对

java - 为什么需要 main 方法才能在类中使用 arraylist 方法?