如何创建一个 android 应用程序以通过 NFC 读取 URL?
How to create an android app to read URLs via NFC?
我尝试创建一个应用程序来读取来自其他设备的 nfc 消息:
AndroidManifest.xml
...
<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" />
我的activity:
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)
}
}
我的技术列表:
<?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 时,activity 不会被调用。有人有解决方案的想法吗?
您可以在这里找到来源:
https://github.com/enthusiasmus/nfc
提前致谢!
NFCActivity 会被调用,但因为它什么都不做,你永远不会知道。
当使用通过 Intents 获取 NFC 数据的旧方法时,NFC 数据有两个可能的入口点。
1) 你的 Activity 已经是 运行 如果你有 enableForegroundDispatch
https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch 然后系统生成的 Intent 将传递给 onNewIntent
但是因为你的 NFCActivity 不是 运行 而你没有 enableForegroundDispatch
那么它不会'不会传递给 onNewIntent
2) 您设置了 NFC Intent 过滤器并且您的应用程序不是 运行 然后您的 activity 是第一次启动然后您将处理 onCreate
中的 Intent 不是在 onNewIntent
这是因为 onNewIntent
的文档说
https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)
when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
因为你 Activity 不是 RE -launched onNewIntent
没有被调用。
我见过的大多数应用程序都将 Intent 处理移至单独的方法
例如readFromIntent
然后当应用程序被 NFC 过滤器启动时,从 onCreate
调用 readFromIntent
要么
从 onNewIntent
调用 readFromIntent
当应用程序已经 运行 并且 enableForegroundDispatch
导致系统将 NFC Intent 发送到已经 运行 的应用程序。
此外,您可能会发现 Intent 不会以 NDEF_DISCOVERED 的形式出现,因为您没有在 NFC 过滤器中设置数据类型
例如
<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/
我尝试创建一个应用程序来读取来自其他设备的 nfc 消息:
AndroidManifest.xml
...
<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" />
我的activity:
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)
}
}
我的技术列表:
<?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 时,activity 不会被调用。有人有解决方案的想法吗?
您可以在这里找到来源: https://github.com/enthusiasmus/nfc
提前致谢!
NFCActivity 会被调用,但因为它什么都不做,你永远不会知道。
当使用通过 Intents 获取 NFC 数据的旧方法时,NFC 数据有两个可能的入口点。
1) 你的 Activity 已经是 运行 如果你有 enableForegroundDispatch
https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch 然后系统生成的 Intent 将传递给 onNewIntent
但是因为你的 NFCActivity 不是 运行 而你没有 enableForegroundDispatch
那么它不会'不会传递给 onNewIntent
2) 您设置了 NFC Intent 过滤器并且您的应用程序不是 运行 然后您的 activity 是第一次启动然后您将处理 onCreate
中的 Intent 不是在 onNewIntent
这是因为 onNewIntent
的文档说
https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)
when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
因为你 Activity 不是 RE -launched onNewIntent
没有被调用。
我见过的大多数应用程序都将 Intent 处理移至单独的方法
例如readFromIntent
然后当应用程序被 NFC 过滤器启动时,从 onCreate
调用 readFromIntent
要么
从 onNewIntent
调用 readFromIntent
当应用程序已经 运行 并且 enableForegroundDispatch
导致系统将 NFC Intent 发送到已经 运行 的应用程序。
此外,您可能会发现 Intent 不会以 NDEF_DISCOVERED 的形式出现,因为您没有在 NFC 过滤器中设置数据类型
例如
<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/