I am writing a camera app, when the phone read a NFC tag, it will takes a photo
I use this example
https://github.com/josnidhin/Android-Camera-Example
And then modify it
1 Add two properties in CamTestActivity
PendingIntent pendingIntent;
Tag tag;
2 add this in onCreate
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
3 add this in onPause
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
4 onResume
Log.v("new intent","resume");
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
5 And then add a new method
@Override
protected void onNewIntent(Intent intent) {
Log.v("new intent","new intent");
//preview.performClick();
}
But it does not work
When it read a NFC tag, it will call pause, new intent, resume. It close the activity and start again, but this time it runs onNewIntent rather than new onCreate
I tried many flag, but no one can keep the activity on foreground
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FILL_IN_ACTION), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK), 0);
I found an App on play store, it is called "NFC Camera", it can read NFC tag without restart activity, how to do that?
mNfcPendingIntent = PendingIntent.getActivity(CamTestActivits.this, 0, new Intent(CamTestActivity.this, CamTestActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
– M Dtag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
– CL So