我有一张智能海报和AAR写的NFC卡
NdefMessage message = new NdefMessage(smartPosterRecord, aarRecord);
当我将此卡附加到设备时,我在AAR中定义的演示应用程序启动,但intent.getAction()
返回android.intent.action.MAIN
而不是NfcAdapter.ACTION_TAG_DISCOVERED
或NfcAdapter.ACTION_TECH_DISCOVERED
,所以我无法获取从中标记。
如何才能从智能海报中读取URL?
NDEF:
NdefRecord uriRecord = NdefRecord.createUri(uri);
NdefMessage message = new NdefMessage(uriRecord);
NdefRecord smartPosterRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_SMART_POSTER, new byte[0], message.toByteArray());
NdefRecord aarRecord = NdefRecord.createApplicationRecord(aar);
message = new NdefMessage(smartPosterRecord, aarRecord);
// Get instance of Ndef for the given tag
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
意图处理:
public void handleIntent(Intent intent) {
if (((NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()))) || (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))) {
// do something
}
清单:
<activity
android:name="com.demo.app.DemoActivity"
android:label="GeneralDemo"
android:launchMode="singleInstance" >
<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"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfcfilter" />
</activity>
答案 0 :(得分:0)
将此添加到清单中就可以了:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="demo.com" />
</intent-filter>
该应用程序不是因为AAR而启动,而是因为“抓住”智能海报的网址......
编辑: 最后,我们决定使用我们在应用程序中捕获的自定义MimeType(通过intent-filter)