Two NFC tags linking to two different activities e

2020-03-30 05:59发布

I'm doing an NFC Application and was wondering if this scenario is possible:

Say, I have 2 NFC tags and 2 activities in one project. NFC A is written to open up Activity A by writing MIME type in NFC A as

application/com.example.hello

In the project's manifest file, Activity A has this intent filter:

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

So, this works perfectly fine. I'm going to add another NFC Tag, and another activity. NFC B is written to open up Activity B

Now, how should I write my MIME type into NFC B and set up the intent-filter for Activity B? Considering Activity A and Activity B are both in one project and package.

If I write the same MIME type in NFC A and B for Activity A and B, I will be asked which activity to open upon tapping and I don't want that.

2条回答
Luminary・发光体
2楼-- · 2020-03-30 06:33

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>
查看更多
时光不老,我们不散
3楼-- · 2020-03-30 06:46

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.

查看更多
登录 后发表回答