Android Nfc Sensing And Read Tag Data At The Same Time
I have a question about the android NFC. I have already done the function about read and write, but still have one problem. I wrote the AAR in my tag, after first sensing, it can l
Solution 1:
Use the below pattern (from here). Summary:
The foreground mode lets you capture scanned tags in the form of intents sent to onNewIntent. An onResume will follow the onNewIntent call, so we'll process the intent there. But onResume can also come from other sources, so we add a boolean variable to make sure we only process each new intent once.
An intent is also present when the activity is launched. By initializing the boolean variable to false, we fit it into the above flow - an your problem should be fixed.
protectedboolean intentProcessed = false; publicvoidonNewIntent(Intent intent) { Log.d(TAG, "onNewIntent"); // onResume gets called after this to handle the intent intentProcessed = false; setIntent(intent); } protectedvoidonResume() { super.onResume(); // your current stuffif(!intentProcessed) { intentProcessed = true; processIntent(); } }
Solution 2:
In AndroidManifest -
<activityandroid:name=".TagDiscoverer"android:alwaysRetainTaskState="true"android:label="@string/app_name"android:launchMode="singleInstance"android:screenOrientation="nosensor" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><actionandroid:name="android.nfc.action.TECH_DISCOVERED" /><actionandroid:name="android.nfc.action.TAG_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:mimeType="text/plain" /></intent-filter><meta-dataandroid:name="android.nfc.action.TECH_DISCOVERED"/></activity>
you should initiate the NFC adopter in OnCreate()..
/**
* Initiates the NFC adapter
*/privatevoidinitNfcAdapter() {
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
Now in OnResume() ...
@OverrideprotectedvoidonResume() {
super.onResume();
if (nfcAdapter != null) {
nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
}
Post a Comment for "Android Nfc Sensing And Read Tag Data At The Same Time"