首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建一个通过NFC读取URL的android应用程序?

如何创建一个通过NFC读取URL的android应用程序?
EN

Stack Overflow用户
提问于 2020-04-11 16:07:03
回答 1查看 457关注 0票数 0

我试图创建一个应用程序来读取来自其他设备的nfc消息:

AndroidManifest.xml

代码语言:javascript
复制
...
    <activity android:name=".NFCActivity">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

    <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/tech_list" />
</application>

<uses-sdk android:minSdkVersion="10"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-permission android:name="android.permission.NFC" />

我的活动:

代码语言:javascript
复制
class NFCActivity : AppCompatActivity() {
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)

        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
                val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }

                println(String(messages[0].records[0].payload));
            }
        }

        val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
        println(tag)
    }
}

我的技术人员:

代码语言:javascript
复制
<?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>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

当发送URL(来自两个不同的设备)时,活动不会被调用。有人想办法解决吗?

您可以在这里找到的源:https://github.com/enthusiasmus/nfc

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-11 17:07:44

NFCActivity将被调用,但是因为它什么都不做,你永远不会知道。

当使用通过意图获取NFC数据的旧样式时,NFC数据有两个可能的入口点。

1)您的活动已经在运行,如果您有enableForegroundDispatch https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch,那么系统生成的意图将传递给onNewIntent,但是由于您的NFCActivity没有运行,并且您没有enableForegroundDispatch,所以它不会传递给onNewIntent

2)您设置了NFC意图筛选器,并且您的应用程序没有运行,那么您的活动将第一次启动,然后您将在onCreate (而不是在onNewIntent )中处理该意图

这是因为onNewIntent的文档

https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

当活动在活动堆栈顶部重新启动时,而不是正在启动的活动的新实例时,将在现有实例上调用

(),其目的是重新启动该实例。

由于您的活动不是、RE、、-launched、onNewIntent,所以不调用它。

我见过的大多数应用程序都将意图处理移动到一个单独的方法上,例如,当应用程序由NFC过滤器启动时,从readFromIntent调用readFromIntent,或者在应用程序已经运行时从onNewIntent call readFromIntentenableForegroundDispatch已经导致系统将NFC意图发送到已经运行的应用程序。

另外,您可能会发现意图不会以NDEF_DISCOVERED的形式出现,因为您的NFC过滤器中没有数据类型集。

例如:

代码语言:javascript
复制
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="*/*" />
</intent-filter>

https://developer.android.com/guide/topics/connectivity/nfc/nfc#ndef-disc中,*/*将获得所有类型,如果需要,可以在以后缩小范围。

https://www.codexpedia.com/android/android-nfc-read-and-write-example/的完整示例

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

https://stackoverflow.com/questions/61159811

复制
相关文章

相似问题

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