android - NFC读卡时,会自行打开应用,请问如何解决?
问题描述
当 NFC 读取卡时,它会自行打开应用程序,但我只想在打开此应用程序时读取 NFC
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
nfc_tech_intent_filter
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
</resources>
解决方案
答案是不要将intent-filter
and tech-filter 放在清单中,这就是导致 NFC 服务在读取匹配的 NFC 卡时打开您的应用程序的原因。
所以从清单中删除
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
当您的应用程序已经运行并在前台时,只需在您的活动(以及禁用的方法)中使用 nfcAdapter.enableForegroundDispatch
甚至更好的 API 来读取 NFC 卡nfcAdapter.enableReaderMode
onResume
onPause
推荐阅读
- azure - 如何在 Powershell 中授予管理员对 Azure AAD 应用程序的许可?
- php - 带有大表的 PHP PDO
- javascript - 单击旁边的按钮时如何交叉?
- python - 在数据框中的两个列表中检测和计数模式
- python - 检查两个函数是否相同
- maven - Intellij 识别 TestNG 注释 @Test 但不识别(优先级 = #)
- android - 底部导航视图图标颜色不准确
- java - 使用 PDFBox 创建文件并下载 [已解决]
- javascript - 即使电话号码正确,asp.net 中的正则表达式验证也会显示错误消息
- c - CMake:如何对多个文件运行自定义命令以生成源文件?