两个NFC标签链接到一个项目中的两个不同活动? [英] Two NFC tags linking to two different activities each in a project?

查看:131
本文介绍了两个NFC标签链接到一个项目中的两个不同活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做NFC应用程序,想知道这种情况是否可行:

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

说,我在一个项目中有2个NFC标签和2个活动. 通过将NFC A中的MIME类型写为

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

在项目的清单文件中,活动A具有以下意图过滤器:

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>

所以,这很好用. 我将添加另一个NFC标签和另一个活动. 编写NFC B是为了打开活动B

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

现在,我应该如何将我的MIME类型写入NFC B并为活动B设置意图过滤器?考虑到活动A和活动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.

如果我在NFC A和B中为活动A和B编写了相同的MIME类型,则系统会询问我在点击时打开哪个活动,我不希望这样做.

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.

推荐答案

根据您要实现的目标,最简单的方法是使用具有两种不同记录类型(例如,两种不同的MIME类型)的两个标签.应该比自定义MIME类型更喜欢使用NFC论坛外部类型名称!)

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!)

假设您有

  • 标签A:

  • Tag A:

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

  • 标签B:

  • Tag B:

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

  • 然后,您可以为活动定义意图过滤器,以便ActivityA仅由标签A触发,而ActivityB仅由标签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>
    

    这篇关于两个NFC标签链接到一个项目中的两个不同活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆