我的Android nfc應用完成讀取智能卡後,會再次顯示相同的NFC標籤信息,然後再次由操作啓動系統。事實上,如果您不移動手機,它會進入永久循環。 正在讀取的數據量需要一到兩秒鐘,因此最終會再次啓動。 (這在我的Galaxy S2薑餅上和我在ICS上的S3上相當常見)如何阻止我的Android NFC應用在接近NFC(智能卡)時循環
當NFC信號源(智能卡)沒有改變並且沒有被移開手機時,我該如何阻止它重複?
我的活動有一個意圖過濾器:
<intent-filter >
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc" />
隨着
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
一個技術列表中的源代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
resolveIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
private void resolveIntent(Intent intent) {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
....work
handled = true;
}
if (!handled) {
Log.e(tag, "Unknown intent " + intent);
finish();
return;
}
}
再次感謝NFC傢伙!這個問題非常困難,因爲某些電話和某些卡片在其他人沒有顯示時顯示 - 我的解決方案如下。我確實使用前臺調度,並且ID讀取並不總是可靠的 - 所以我必須利用延時來忽略其他請求。一旦第二個被忽略,它似乎停止了永恆的循環! – paul 2012-08-16 11:18:09