为从另一个应用启动的活动启用前景调度



我开发了一个具有NFC标签交互的应用程序。大多数情况下,应用程序必须忽略标记发现事件。当我的应用处于前台时,其他应用不应捕获标记迪斯科事件。所以我使用EnableForegroundDispatch来处理这个问题。

这工作正常。我将多余的TAG_DISCOVERED意图处理到活动的 onNewIntent 方法中。

问题:当我的活动从另一个应用程序启动时,enableForegroundDispatch不起作用。我在新活动的 onCreate 方法中收到TAG_DISCOVERED意图。 这是我的前景调度

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);
}

可能与旗帜有关?请帮忙

我的经验是,如果您启用带有所有标志的 ReaderMode(包括跳过 NDEF 检查(,那么如果在前台运行,基本上任何卡类型的检测都会发送到您的应用程序。

除了在简历中enableReaderMode,我还有广播接收器来启用NFC服务状态更改的阅读器模式。

检查许多不同的标签,这似乎有效,但它们都是基于不同格式的 NfcA。正如 stackoverflow.com/questions/33633736/所证实的那样...但似乎 Android 10 可能会出现信息亭模式的计时问题

最新更新