gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:15:04 31 4
gpt4 key购买 nike

我一直在我的应用程序中检测支持 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com