broadcastreceiver - How to make android application listen to specific NFC chip? -
i'm creating application action if hit nfc chip , know following information nfc in android please correct me if wrong
- you can't register actions of nfc receiver in manifest file , activities .
- each nfc chip has it's own unique id .
what want is
while application in background or it's closed , if hit nfc chip id (ex 1234) app lunched , action.
is possible ? if yes, how can achieved?
edit here code , open when check nfc chip , action android.nfc.action.tech_discovered
when open action android.intent.action.main
mainactivity.java
public class mainactivity extends appcompatactivity { public static final string action_check_in= "checked"; public static final string action_check_out= "unchecked"; private nfcadapter mnfcadapter; boolean ischeckedin = false; private static final string tag = "mainactivity"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnfcadapter = nfcadapter.getdefaultadapter(this); ischeckedin = pref.getboolean("check", false); } @override protected void onresume() { super.onresume(); log.d(tag, "onresume() called with: " + ""); string action = getintent().getaction(); if (action != null) { log.d("mainactivity", "oncreate(" + action + ")"); if (action.equals(nfcadapter.action_ndef_discovered) || action.equals(nfcadapter.action_tag_discovered) || action.equals(nfcadapter.action_tech_discovered)) { if (!ischeckedin) { ischeckedin = true; pref.putboolean("check", true); log.d("checking","in"); } else { ischeckedin = false; pref.putboolean("check", false); log.d("checking","out"); } } } if (!mnfcadapter.isenabled()) { toast.maketext(mainactivity.this, "please open ", toast.length_short).show(); } /** * it's important, activity in foreground. * otherwise illegalstateexception thrown. */ setupforegrounddispatch(this, mnfcadapter); } @override protected void onpause() { /** * call before onpause, otherwise illegalargumentexception thrown. */ stopforegrounddispatch(this, mnfcadapter); super.onpause(); } @override protected void ondestroy() { super.ondestroy(); } public static void setupforegrounddispatch(activity activity, nfcadapter adapter) { final intent intent = new intent(activity.getapplicationcontext(), activity.getclass()); intent.setflags(intent.flag_activity_single_top); final pendingintent pendingintent = pendingintent.getactivity( activity.getapplicationcontext(), 0, intent, 0); intentfilter[] filters = new intentfilter[2]; string[][] techlist = new string[][]{}; // same filter manifest - action, category filters[0] = new intentfilter(); filters[0].addaction(nfcadapter.action_tag_discovered); filters[1] = new intentfilter(); filters[1].addaction(nfcadapter.action_tech_discovered); adapter.enableforegrounddispatch(activity, pendingintent, filters, techlist); } public static void stopforegrounddispatch(activity activity, nfcadapter adapter) { adapter.disableforegrounddispatch(activity); } }
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.srjo.pocnfcadapter"> <uses-permission android:name="android.permission.nfc" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <application android:name=".myapplication" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <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.tag_discovered" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.tech_discovered" /> </intent-filter> <meta-data android:name="android.nfc.action.tech_discovered" android:resource="@xml/filter_nfc" /> </activity> </application> </manifest>
according the android developer site, possible app filter nfc intents such action_ndef_discovered.
Comments
Post a Comment