我正在尝试通过扫描标签来启动我的应用程序,但我得到的意图是主动作而不是我想要发现的NDEF。
这是我用来编写NdefMessage的代码:
NdefRecord aar = NdefRecord.createApplicationRecord("com.example.mynfc");
NdefRecord record = createUriRecord("www.stackoverflow.com");
NdefMessage message = new NdefMessage(new NdefRecord[]{record, aar});
使用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;
}
以下是使用intent过滤器的清单的一部分:
<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>
我做错了什么? aar记录放在第2位,意图过滤器考虑所有mime类型。 thx的帮助
答案 0 :(得分:1)
在过滤MIME类型记录时,您正在创建URI类型记录。而是过滤URI类型的记录。除此之外,URI记录有效负载应该以一个方案开头,例如“http://”(否则过滤器永远不会匹配)。