当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>发布于 2020-10-08 16:16:19
答案是不要将intent-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或更好的nfcAdapter.enableReaderMode API(以及onPause中的禁用方法),当应用程序已经运行并处于前台时,就可以读取NFC卡了。
https://stackoverflow.com/questions/64263356
复制相似问题