Launching app from NFC tag reading

て烟熏妆下的殇ゞ 提交于 2019-12-08 09:31:38

问题


I've read and read, but I can't understand what do I need to do exactly to make the application launch when I touch a NFC tag (a list pops with TagWriter and on{X})

I've added this to my applications Main Activity (in Manifest)

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>

And I also write and read the tag with application/com.pgsideris.aeglea NDEF Data within the application

I would welcome some detail, as most other posts don't offer much and the links they provide have confused me somewhat.


回答1:


The main reason for those applications to pop-up is you are filtering for mimetype "text/plain" and if you wrote an NFC-tag with that mimetype all applications including your own application will respond to the tag.

If you are correctly writing the NFC-tag, perhaps post some code to verify that, you should filter for mimetype "application/com.pgsideris.aeglea" this results in the following filter:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="application/com.pgsideris.aeglea" />
</intent-filter>



回答2:


Does your app need to read the tag, or do you only need to start it? If the latter, Android provides a custom NDEF rectord in which you can specify the exact package (app) to start. Cf, Android Application Record (AAR): http://developer.android.com/reference/android/nfc/NdefRecord.html#createApplicationRecord(java.lang.String)

If you want to read the tag, and your app is not in the foreground you will get a selection dialog if other apps on the device have registered to handle the same tag, NFC technology, etc. You cannot change this, only make sure your app gets priority if it is already in the foreground by using foreground dispatch.




回答3:


Make sure to add the permission to AndroidManifest.xml:

<uses-permission android:name="android.permission.NFC"></uses-permission>

Depending on the type of tag, you may need to filter the tag. For example, to filter for tags that contain urls pointing to yourserver, you would put this in your intent-filter:

           <data android:scheme="http"
            android:host="yourserver.com"
            android:pathPrefix="/"/>


来源:https://stackoverflow.com/questions/13980687/launching-app-from-nfc-tag-reading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!