Application launched by Action Main (from AAR) ins

2019-06-09 14:47发布

问题:

I'm trying to launch my application by scanning a tag, but the intent I get is the action Main and not the NDEF Discovered as I would like.

Here is my code I use to write the NdefMessage :

NdefRecord aar = NdefRecord.createApplicationRecord("com.example.mynfc");
NdefRecord record = createUriRecord("www.stackoverflow.com");
NdefMessage message = new NdefMessage(new NdefRecord[]{record, aar});

with the createUriRecord :

private NdefRecord createUriRecord(String text){
    String uniqueId = text.substring(4);      
    byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII"));  
    byte[] payload = new byte[uriField.length+1];                                                                       //add 1 for the URI Prefix  
    payload[0] = 0x01;                                                                                                  //prefixes http://www. to the URI  
    System.arraycopy(uriField, 0, payload, 1, uriField.length);                                                         //appends URI to payload  
    NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
    return uriRecord;
}

And here is the part of the manifest with the intent filter :

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <activity
        android:name="com.example.mynfc.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/techs" />

        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

What am I doing wrong ? The aar record is placed 2nd, and the intent filter takes into account all mime types. thx for the help

回答1:

You are creating an URI-type record while filtering for MIME-type records. Filter for URI-type records instead. Besides that, the URI record payload should start with a scheme, such as "http://" (otherwise the filter will never match).