Skip to content Skip to sidebar Skip to footer

Launch Specific App When Nfc Is Discovered

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

Solution 1:

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. 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:

    NdefRecordextRecord= 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><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:scheme="vnd.android.nfc"android:host="ext"android:pathPrefix="/yourdomain.com:yourtype" /></intent-filter></activity>
  2. 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:

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

Solution 2:

Use the External Type NDEF record with your own domain and give your app a corresponding intent-filter.

Post a Comment for "Launch Specific App When Nfc Is Discovered"