【发布时间】:2014-11-05 08:11:55
【问题描述】:
我正在使用 NFC 并且正在处理意图。发现 NFC 标签时 我开始我的 NFCActivity。
<activity
android:name="controller.NFCActivity"
android:launchMode="singleTask" >
<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/nfc_tech_filter" />
</activity>
NFCActivity 是一个没有 UI 的 Activity。它启动了我的 NFCReaderTask。
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NFCDataDevice dataDevice = new NFCDataDevice();
dataDevice.setCurrentTag(tagFromIntent);
byte[] systemInfoAnswer = NFCCommand.sendGetSystemInfoCommandCustom(tagFromIntent, new NFCDataDevice());
if (NDEFHelper.decodeSystemInfoResponse(systemInfoAnswer, new NFCDataDevice())) {
NFCReaderTask nfcReaderTask = new NFCReaderTask(this, this, dataDevice);
nfcReaderTask.execute();
}
}
当 ReaderTask 完成时,我的回调方法 onNFCReaderTaskCompleted 在我的
NFCActivity 被调用,我这样做:
public void onNFCReaderTaskCompleted(Long time, String value) {
Intent newIntent = new Intent(this.getApplicationContext(), NFCResultActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.putExtra(Globals.KEY_NFC, true);
newIntent.putExtra(Globals.KEY_TIME, time);
newIntent.putExtra(Globals.KEY_VALUE, value);
this.startActivity(newIntent);
this.finish();
}
第一次发现 NFC 标签时很好。我现在想要的是不要 总是启动一个新的 NFCResultActivity。当 NFCResultActivity 处于活动状态时,我只是 想要调用该 NFCResultActivity 的方法来设置新数据而不是完全 创建一个新的活动。
我怎样才能做到这一点?
【问题讨论】: