发现NFC时启动特定的应用程序 [英] Launch Specific App when NFC is discovered

查看:1479
本文介绍了发现NFC时启动特定的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用NFC,并且工作正常.但是,我想确保仅启动了我的应用程序,并且没有其他应用程序可以处理此意图.以下是清单文件中的代码:

I am using NFC in my app and it is working fine. However I want to make sure that only my app is launched and no other App is there to handle the intent. Following is the code for it in my Manifest file:

<activity android:name="com.mypackage.name.BeamActivity">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

我的手机上还有另一个示例应用程序,该应用程序正在检测NFC意图并向我提供意图选择器.以下是清单文件中的代码.

I have another sample app on my phone which is detecting NFC Intent and providing me Intent Chooser. Following is the code for it in Manifest file.

<activity android:name="com.package2.name.NFCStickyNotesActivity"  android:label="Sticky Notes" >
    <!-- Handle notes detected from outside our application -->
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

当我的应用程序从另一台设备推送过来时,我希望它成为处理特定NFC意图的唯一应用程序.

I would like my App to be the only app to handle the particular NFC Intent when my App push it across from another device.

我不确定是否必须在清单文件或代码中做特定的事情.感谢您的帮助.

I am not sure whether I have to do something specific in the manifest file or in the code. Any help is appreciated.

推荐答案

获得意图选择器的原因是,针对数据类型text/plain注册了多个活动.这是一种很常见的情况,因此您应避免将此类通用数据类型用于应启动您的活动的NDEF记录.您有两种选择可以解决此问题:

The reason why you get an intent chooser is that multiple activities are registered for the data type text/plain. This is a rather common case and you should therefore avoid using such generic data types for the NDEF record that should launch your activity. You have two options to overcome this problem:

  1. 为您的NDEF记录使用NFC论坛外部类型(这已经提到了 ThomasRS ).使用此方法,您可以创建仅对您的应用程序有意义的自定义记录类型.您可以使用以下内容创建这样的记录(将其写入标签或通过Beam发送):

  1. Use an NFC Forum external type for your NDEF record (this is what ThomasRS already mentioned). With this method you create a custom record type that is meaningful to your application only. You can create such a record (to write it to your tag or to send over Beam) with something like this:

NdefRecord extRecord = NdefRecord.createExternal(
        "yourdomain.com",  // your domain name
        "yourtype",        // your type name
        textBytes);        // payload

然后您可以注册活动以像下面这样在此记录上启动:

You can then register your activity to launch upon this record like this:

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="vnd.android.nfc" android:host="ext"
              android:pathPrefix="/yourdomain.com:yourtype" />
    </intent-filter>
</activity>

  • 使用Android应用程序记录(AAR). AAR将确保NDEF_DISCOVERED意图仅通过特定的程序包名称传递给应用程序.您可以使用以下内容创建这样的记录(将其写入标签或通过Beam发送):

  • Use an Android Application Record (AAR). An AAR will make sure that the NDEF_DISCOVERED intent is delivered to an app with a specific package name only. You can create such a record (to write it to your tag or to send over Beam) with something like this:

    NdefRecord appRecord = NdefRecord.createApplicationRecord(
            "com.yourdomain.yourapp");
    NdefRecord textRecord = NdefRecord.createTextRecord(
            "en",       // language code
            "yourtext"  // human-readable text);
    NdefMessage msg = new NdefMessage(
            textRecord,
            appRecord);  // use the AAR as the *last* record in your NDEF message
    

  • 这篇关于发现NFC时启动特定的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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