Two NFC tags linking to two different activities each in a project?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 12:30:16

One way might be to have two MimeTypes in your manifest. Then you have those MimeTypes linked to an (additional) activity which will check which MimeType is actually on your tag (A or B). Depending on what you find you can lauch the respective activity A or B.

Depending on what you want to achieve, the easiest way would be to use two tags with two different record types (e.g. two different MIME types, but note that you should prefer to use NFC Forum external type names over custom MIME types!)

Assuming you have

  • Tag A:

    +--------------------------------------+
    | MIME:application/com.example.hello.a |
    +--------------------------------------+
    
  • Tag B:

    +--------------------------------------+
    | MIME:application/com.example.hello.b |
    +--------------------------------------+
    

Then you can define intent filters for your activities, so that ActivityA will only be triggered by tag A and ActivityB will only be triggered by tag B:

<activity android:name=".ActivityA" ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/com.example.hello.a" />
    </intent-filter>
</activity>

<activity android:name=".ActivityB" ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/com.example.hello.b" />
    </intent-filter>
</activity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!