java - Using QR and NFC technology together in an Android app -
i developing android app consisting of 2 activites far. first activity (mainactivity) started when app launched or when qr code scanned. mainactivity starts second activity (nfcactivity) when user presses button. nfcactivity waits user tap nfc token, reads out data token, , returns read data mainactivity.
this works fine if app started manually. if app started scanning qr code, taping nfc tag not invoke nfcactivity's onnewintent() method exepcted, instead creates new instance of nfcactivity on top of displayed one.
the enableforegrounddispatch() method called , flag_activity_single_top should set. relevant source code of minimal example provided below. highly appreciated!
mainactivity:
public class mainactivity extends activity { private edittext dataread; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_start); dataread = (edittext) findviewbyid(r.id.data); final button readkeybutton = (button) findviewbyid(r.id.readnfc); readkeybutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent keyintent = new intent(mainactivity.this, nfcactivity.class); keyintent.addflags(intent.flag_activity_single_top); startactivityforresult(keyintent, 1); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent intent) { if (requestcode == 1) { string result = intent.getextras().getstring("resultdata"); this.dataread.settext(result); } } }
main activity's gui:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <button android:id="@+id/readnfc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="127dp" android:text="read nfc tag" /> <edittext android:id="@+id/data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/readnfc" android:layout_centerhorizontal="true" android:layout_marginbottom="85dp" android:ems="10" > <requestfocus /> </edittext> </relativelayout>
nfcactivity:
public class nfcactivity extends activity { private nfcadapter madapter; private pendingintent pendingintent; private intentfilter[] mfilters; private string[][] mtechlists; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.read_nfc); madapter = nfcadapter.getdefaultadapter(this); pendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); // // setup intent filter mime based dispatches intentfilter ndef = new intentfilter(nfcadapter.action_ndef_discovered); try { ndef.adddatatype("*/*"); } catch (malformedmimetypeexception e) { throw new runtimeexception("fail", e); } intentfilter td = new intentfilter(nfcadapter.action_tag_discovered); mfilters = new intentfilter[] { ndef, td }; // // // setup tech list nfcf tags mtechlists = new string[][] { new string[] { nfcv.class.getname(), nfcf.class.getname(), nfca.class.getname(), nfcb.class.getname() } }; } @override public void onresume() { super.onresume(); if (madapter != null) madapter.enableforegrounddispatch(this, pendingintent, mfilters, mtechlists); } @override public void onpause() { super.onpause(); if (madapter != null) madapter.disableforegrounddispatch(this); } @override public void onnewintent(intent intent) { log.d("test", "onnewintent() called."); // read nfc tag here [skipped minimal example] // return dummy data test intent result = new intent(); result.putextra("resultdata", "dummy data"); setresult(1, result); finish(); } }
nfcactivity's gui:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#303030" android:paddingleft="30dp" android:paddingright="30dp" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="tap nfc tag.." android:textappearance="?android:attr/textappearancelarge" android:textcolor="#ff8811" android:textsize="30sp" android:textstyle="bold" /> </relativelayout>
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.nfcqrtest" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="16" android:targetsdkversion="18" /> <uses-permission android:name="android.permission.nfc" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="test.nfcqrtest.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.intent.action.view" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.browsable" /> <data android:scheme="myapp" android:host="test.org" android:pathprefix="/testapp" /> </intent-filter> </activity> <activity android:name=".nfcactivity" android:windowsoftinputmode="statehidden" android:screenorientation="portrait" android:launchmode="singletop"> </activity> </application> </manifest>
in case facing similar problem: able overcome issue described above setting android:launchmode
property mainactivity
singleinstance
.
Comments
Post a Comment