NFC read TAGS in differents Activities automaticly without ask where process the TAGS

I want to read in different Activites any TAG without appear in my display, what Activity is the best for read this tag? I think this should be automatic. My code manifest is this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.iprocuratio.strim.LoginActivity"
        android:label="@string/title_activity_login" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.iprocuratio.strim.TemperatureActivity"
        android:label="@string/title_activity_temperature" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.ConstantsActivity"
        android:label="@string/title_activity_constants" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.FC"
        android:label="@string/title_activity_fc" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.FR"
        android:label="@string/title_activity_fr" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.SatO2"
        android:label="@string/title_activity_sat_o2" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.PrescriptionsActivity"
        android:label="@string/title_activity_prescriptions">
        <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>
    <activity
        android:name="com.iprocuratio.strim.MedicamentosActivity"
        android:label="@string/title_activity_medicamentos" >
    </activity>
    <activity
        android:name="com.iprocuratio.strim.Identificacion"
        android:label="@string/title_activity_identificacion" >
        <intent-filter >
            <action android:name="com.google.zxing.client.android.SCAN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.iprocuratio.strim.Pantallaleer"
        android:label="@string/title_activity_pantallaleer" >
        <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>
    <activity
        android:name="com.iprocuratio.strim.IdentificacionFinal"
        android:label="@string/title_activity_identificacion_final" >

    </activity>
</application>

I have implemented the read the TAG in two activities: PrescriptionsActivity and Pantallaleer. For example, when I want to read a TAG in PrescriptionsActivity always appear the next image: enter image description here

I would like that when I read the TAG it doesn't show this window and it automaticly choose the correct activity. Anyone know, how can I do? Is my code correct?

Let me know. Thanks.

Regards.

Upvotes: 0

Views: 98

Answers (1)

Michael Roland
Michael Roland

Reputation: 40831

Both activities are sensitive on the same intent (i.e. both trigger on tags that contain a Text record (or a MIME type record of type text/plain):

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

It's impossible for Android to tell what activity would better fit the needs for a specific tag if both contain the same data type and both filters match the same data type. Consequently Android shows the intent chooser to let the user select.

If you want to avoid the intent chooser, you should use different data types for each activity (insead of using the very unspecific text/plain data type). For instance, if a tag should launch PrescriptionsActivity, you would write the following NDEF message to the tag:

+-------------------------------------------------+
| EXT:iprocuratio.com:prescriptions | <your data> |
+-------------------------------------------------+

You could create this NDEF message on Android using

NdefMessage msg = new NdefMessage(
    NdefRecord.createExternal("iprocuratio.com", "prescriptions", yourData),
);

The intent filter for PrescriptionsActivity could then look like this:

<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="/iprocuratio.com:prescriptions"/>
</intent-filter>

Similarly, for the Pantallaleer activity, you could use:

+-------------------------------------------------+
| EXT:iprocuratio.com:pantallaleer | <your data> |
+-------------------------------------------------+

You could create this NDEF message on Android using

NdefMessage msg = new NdefMessage(
    NdefRecord.createExternal("iprocuratio.com", "pantallaleer", yourData),
);

The intent filter for Pantallaleer activity could then look like this:

<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="/iprocuratio.com:pantallaleer"/>
</intent-filter>

Upvotes: 1

Related Questions