我有一张 NFC 卡,上面写有 Smart Poster 和 AAR
NdefMessage message = new NdefMessage(smartPosterRecord, aarRecord);
当我将此卡附加到设备时,我在 AAR 中定义的演示应用程序启动,但intent.getAction()
返回android.intent.action.MAIN
而不是NfcAdapter.ACTION_TAG_DISCOVERED
或者NfcAdapter.ACTION_TECH_DISCOVERED
我无法从中获取标签。
应该怎么做才能从智能海报中读取 URL?
定义:
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>