android - NFC Tag is null on onNewIntent() -
i have been developing app uses nfc tags magic.
all has been until when changed code thats not related of nfc code has been working.
when launch application via nfc tap, works , recieve future nfctag's in onnewintent() tap whilst application running.
when launch application via icon , attempt tap while application running, onnewintent() method called, when try nfctag intent returns null.
am correct in thinking though null, have set foregrounddispatch correctly since onnewintent() called?
heres code...
protected void onresume() { if(this.mnfcadapter==null) { mnfcadapter = nfcadapter.getdefaultadapter(this); } pendingintent pendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); intentfilter nfcfilter = new intentfilter(nfcadapter.action_ndef_discovered); try { nfcfilter.adddatatype("application/application.myorg.myapp"); } catch (malformedmimetypeexception e) { log.e(tag, "error setting fd nfc", e); } string[][] mtechlists = new string[][] { new string[] { nfcf.class.getname() } }; mnfcadapter.enableforegrounddispatch(this, pendingintent, new intentfilter[] {nfcfilter}, mtechlists); } protected void onpause() { super.onpause(); mnfcadapter.disableforegrounddispatch(this); log.d(tag, "activity pausing"); } protected void onnewintent(intent intent) { log.d(tag, "nfc tap while active"); tag tag = getintent().getparcelableextra(nfcadapter.extra_tag); if(tag!=null) { //never called when launched via icon (not nfc) log.d(tag, "tag not null"); } }
the mime type set in intentfilter same have in manifest.
edit
my manifest
<activity android:name="org.mypackage.myapp.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.ndef_discovered" /> <data android:mimetype="application/org.mypackage.myapp" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity>
what tag looks like
+---------------------------------------------------+ | mime:application/org.mypackage.myapp | stringdata | +---------------------------------------------------+ | ext:android:com:pkg | org.mypackage.myapp | +---------------------------------------------------+
the problem how retrieve intent in onnewintent()
method. using getintent()
intent try retrieve extra_tag
. unless use setintent(...)
change (what don't in relevant part of code), return intent initially started activity. nfc discovery intent passed onnewintent()
method in parameter intent intent
. using should trick:
protected void onnewintent(intent intent) { log.d(tag, "nfc tap while active"); tag tag = intent.getparcelableextra(nfcadapter.extra_tag); if (tag != null) { log.d(tag, "tag not null"); } }
moreover, might want check if intent received has intent action expect (e.g. action_ndef_discovered
).
one more thing: can safely set mtechlists
null
when register ndef_discovered
intents. tech list used tech_discovered
intent filters.
Comments
Post a Comment