问题
I have 2 issues with reading NFC Tags.
First is Tag Read Activity creates each time when tag received.
And second issue is activity opens in full screen window, not under Tab Host Activity, but first issue is worst.
What do I do ( AndroidManifest.xml ):
<activity
android:name="readingActivity" >
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and readingActivity.cs:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("W", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.readingActivity);
}
@Override
public void onResume() {
super.onResume();
Log.d("W", "onResume");
PendingIntent intent = PendingIntent.getActivity(this, 0, getIntent(), 0);
NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, intent,
null, null);
}
@Override
protected void onPause() {
super.onPause();
if(NfcAdapter.getDefaultAdapter(this) != null)
NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}
Logs:
02-28 18:22:19.949: D/W(4513): onCreate
02-28 18:22:19.949: D/W(4513): onResume
02-28 18:22:21.078: D/W(4513): onCreate
02-28 18:22:21.082: D/W(4513): onResume
回答1:
The problem is in the PendingIntent
. The getIntent()
retrieves the Intent
that started your Activity
, so passing it to the PendingIntent
will result in starting it another time.
Instead of getIntent()
use something like new Intent(this.getApplicationContext(), this.getClass())
.
来源:https://stackoverflow.com/questions/9485819/android-nfc-read-tags-issue-activity-starts-each-time-on-data-received