【发布时间】:2020-05-25 09:35:17
【问题描述】:
我开发了一个带有 NFC 标签交互的应用。大多数时候应用程序必须忽略标签发现事件。当我的应用程序处于前台时,其他应用程序不应该捕获标签 discoery 事件。所以我使用 EnableForegroundDispatch 来处理。
这很好用。我将多余的 TAG_DISCOVERED 意图处理到我的活动的 onNewIntent 方法中。
问题:当我的活动从另一个应用程序启动时,enableForegroundDispatch 不起作用。我在新活动的 onCreate 方法中收到 TAG_DISCOVERED 意图。 这是我的foregroundDispatch
if (nfcAdapter == null) {
Log.w(TAG, "enableForegroundNfcDispatch: no nfcAdapter", new Exception());
return;
}
Log.d(TAG, "enableForegroundNfcDispatch: ");
if (!nfcMonopolyMode) {
if (nfcTagDiscoveryPendingIntent != null) {
nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);
return;
}
IntentFilter discovery = new IntentFilter(ACTION_TAG_DISCOVERED);
nfcTagDiscoveryFilter = new IntentFilter[]{discovery};
Intent nfcProcessIntent = new Intent(getBaseContext(), getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
nfcTagDiscoveryPendingIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE_NFC_DISCOVERED_TAG, nfcProcessIntent, 0);
nfcMonopolyMode = true;
nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);
}
可能与标志有关?请帮忙
【问题讨论】:
-
通过什么方法启用enableForegroundDispatch? OnResume,OnCreate?另外,我永远不会使用 enableForegroundDispatch,除非目标低于 API 19
enableReaderMode是一个更好的 API -
你好安德鲁!谢谢您的回答!我只使用 EnableForegroundDispatch 来避免其他应用程序捕获 TAG_DISCOVERED 意图。出于阅读标签的目的,我使用 enableReaderMode。我在 onResume 中使用 foregroundDispatch
-
我的经验是,如果您
enableReaderMode带有所有标志(包括跳过 NDEF 检查),那么如果在前台运行,基本上任何卡类型的检测都会发送到您的应用程序。以及在onResume中,我有广播接收器到enableReaderMode的NFC 服务状态更改。检查许多不同的标签,这似乎可行,但它们都是基于不同格式的 NfcA。正如stackoverflow.com/questions/33633736/… 所证实的那样,但似乎 Android 10 的 Kiosk 模式可能存在时间问题 -
安德鲁谢谢。它也适用于我。移动到 onResume 方法中的 nfcAdapter.enableReaderMode ,一切都变得很好。如何在此处将您的答案标记为答案?
-
已将其移至您的答案。