【问题标题】:When NFC reads the card, it opens the application by itself, how can I solve this problem?NFC读卡时会自行打开应用程序,请问如何解决?
【发布时间】:2021-01-23 13:12:03
【问题描述】:

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>

【问题讨论】:

  • 您能详细说明您期望的变化吗?
  • 我不知道,只分享代码。我的问题是当 NFC 读取卡时,它会打开应用程序。我想在用户登录时读取 NFC。

标签: android android-studio nfc ndef cardreader


【解决方案1】:

答案是不要将intent-filter 和 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" />

当您的应用程序已经运行并在前台时,只需在您的活动onResume(以及onPause 中禁用的方法)中使用 nfcAdapter.enableForegroundDispatch 甚至更好的nfcAdapter.enableReaderMode API 来读取 NFC 卡

【讨论】:

  • 非常感谢,当我删除清单中的意图过滤器和技术过滤器时,这个解决方案对我有用,所以这个问题被你解决了,再次非常感谢。
猜你喜欢
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
相关资源
最近更新 更多