【发布时间】:2013-08-14 10:26:34
【问题描述】:
我有一张 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?
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>
【问题讨论】:
标签: android android-intent nfc ndef android-applicationrecord