首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >空NFC标记打开应用程序..。即使它已经打开了。我怎么才能阻止这一切?

空NFC标记打开应用程序..。即使它已经打开了。我怎么才能阻止这一切?
EN

Stack Overflow用户
提问于 2022-02-20 09:35:33
回答 1查看 163关注 0票数 0

我有一个读取NFC标记的应用程序。它很好地读取它们--这意味着打开的应用程序读取标记而不再次打开应用程序--除非NFC标记是空的。在本例中,NFC标记打开应用程序,该应用程序已经打开。那么我们有两个相同的应用程序。

我的目标是连接到空的NFC标记,并使当前打开的应用程序能够写入它。

来自AndroidManifest.xml的片段

代码语言:javascript
复制
        <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>

启用前台调度方法:

代码语言:javascript
复制
    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列出了一切!

代码语言:javascript
复制
<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>

我到底错过了什么?

EN

回答 1

Stack Overflow用户

发布于 2022-02-20 12:17:56

通常情况下,如果您在清单中定义了一个技术列表,那么您还需要一个过滤器。

尝试将您的清单片段更改为

代码语言:javascript
复制
<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>>()

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71193101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档