您好,我正在开发一个使用 NFC 功能的 Android 应用程序。在这里,我尝试读取 NFC Mifare nfc 标签。我使用了 android api 可用的 NFCDemo。但是我没有成功通过我的应用程序读取数据。在那个总是读取假标签并只给出假结果的演示中。
我对 list 文件 Intent 过滤器感到困惑。在我的演示应用程序中是这样的
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc"
>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
>
<activity android:name=".simulator.FakeTagsActivity"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="TagViewer"
android:theme="@android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
并且应用程序总是从 FakeTagsActivity Activity 开始,任何显示假标签列表。当我们单击任何一个假标签时,它会重定向到 TagViewer Activity ,其中包含假数据而不是真实标签数据。 我对 TagViewer Activity 也感到困惑,也就是 resolveIntent(Intent intent) 总是处理
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {}
但我不明白这有什么问题。 请告诉我我是否需要修改 api 演示以读取真实的标签数据。或者建议我有没有其他方法可以读取 nfc 标签。
我已经试过了 This Sample 但对我来说无济于事。 请帮我。提前致谢。
最佳答案
我终于找到了解决问题的方法。我们需要更新 TagViewer Activity 的 Intent 过滤器,例如
<activity android:name="TagViewer"
android:theme="@android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
然后我修改了 TagViewer 类的 if 条件,如
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {}
它现在可以正常工作了。主要问题是 TAG 检测的优先级是 优先级 1:NDEF_DISCOVERED 优先级 2:TECH_DISCOVERED 优先级 3:TAG_DISCOVERED
我为我的应用程序 Intent 过滤器设置了优先级 1,然后 android 系统总是在检测到标记时启动我的 Activity 。
关于android - 我如何在android中读取NFC标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9988011/