我有一个读取NFC标记的应用程序。它很好地读取它们--这意味着打开的应用程序读取标记而不再次打开应用程序--除非NFC标记是空的。在本例中,NFC标记打开应用程序,该应用程序已经打开。那么我们有两个相同的应用程序。
我的目标是连接到空的NFC标记,并使当前打开的应用程序能够写入它。
来自AndroidManifest.xml的片段
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>启用前台调度方法:
private fun enableForegroundDispatch(activity: AppCompatActivity, adapter: NfcAdapter?) {
val intent = Intent(activity.applicationContext, activity.javaClass)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = PendingIntent.getActivity(activity.applicationContext, 0, intent, 0)
val filters = arrayOfNulls<IntentFilter>(1)
val techList = arrayOf<Array<String>>()
filters[0] = IntentFilter()
with(filters[0]) {
this?.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED)
this?.addAction(NfcAdapter.ACTION_TECH_DISCOVERED)
this?.addCategory(Intent.CATEGORY_DEFAULT)
try {
this?.addDataType("*/*")
} catch (ex: IntentFilter.MalformedMimeTypeException) {
throw RuntimeException("Check your MIME type")
}
}
adapter?.enableForegroundDispatch(activity, pendingIntent, filters, techList)
}我的nfc_tech_filter.xml列出了一切!
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>我到底错过了什么?
发布于 2022-02-20 12:17:56
通常情况下,如果您在清单中定义了一个技术列表,那么您还需要一个过滤器。
尝试将您的清单片段更改为
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>但我认为真正的问题是,您的techlist似乎是空的(而且您也没有对TAG_DISCOVERED进行过滤),作为退步,它返回到意图调度系统,它将尝试基于清单启动另一个实例。
来自10亿
如果不匹配,则前台调度系统将返回到意图调度系统。
所以也试着改变val techList = arrayOf<Array<String>>()
至
val techList = arrayOf(arrayOf<String>(NfcA::class.java.name,
NfcB::class.java.name,
NfcF::class.java.name,
NfcV::class.java.name,
Ndef::class.java.name,
NdefFormatable::class.java.name,
MifareClassic::class.java.name,
MifareUltralight::class.java.name,
IsoDep::class.java.name
))希望这在Kotlin中是正确的,我已经有一段时间没有使用enableForegroundDispatch了,因为它存在各种问题,相反,我使用了更好的API enableReaderMode来代替enableForegroundDispatch。
https://stackoverflow.com/questions/71193101
复制相似问题