Android NFC intent handling, showing a toast -
Android NFC intent handling, showing a toast -
hello fellow programmers!
i new android, 2 weeks old.
i trying programming nfc. have nexus 5 , trying read , display uid of mifare classic 1k. know protocol compatibilities issues broadcom chip, can skip , go straight fact can read uid no problem.
i want grab intent , show toast read uid. far made work putting performintent oncreate method. making intent restart activity able handle intent , show uid via toast , works. here humble code: mainactivity.java
package sanjin.com.nfc; import android.content.intent; import android.nfc.nfcadapter; import android.app.activity; import android.nfc.tag; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class mainactivity extends activity { nfcadapter mnfcadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnfcadapter = nfcadapter.getdefaultadapter(this); boolean nfcenabled = mnfcadapter.isenabled(); if (nfcenabled){ toast.maketext(mainactivity.this, r.string.turned_on, toast.length_short).show(); } else { toast.maketext(mainactivity.this, r.string.turned_off, toast.length_short).show(); } performintent(getintent()); } private string serialid = ""; @override public void onresume() { super.onresume(); performintent(getintent()); } private void performintent(intent intent) { string action = intent.getaction(); tag tag = intent.getparcelableextra(nfcadapter.extra_tag); if (action.equals(nfcadapter.action_tag_discovered)) { seek { byte[] tagid = tag.getid(); serialid = tohexstring(tagid); log.d("[readcardtools]", "serial number: " + serialid); toast.maketext(this, serialid,toast.length_short).show(); } grab (nullpointerexception ex) { ex.printstacktrace(); serialid = "error"; } } } public static string tohexstring(byte[] bytes) { char[] hexarray = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; char[] hexchars = new char[bytes.length * 2]; int v; ( int j = 0; j < bytes.length; j++ ) { v = bytes[j] & 0xff; hexchars[j*2] = hexarray[v/16]; hexchars[j*2 + 1] = hexarray[v%16]; } homecoming new string(hexchars); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } }
and manifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sanjin.com.nfc"> <uses-permission android:name="android.permission.nfc" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <application android:label="@string/app_name" android:launchmode="singletask"> <activity android:name=".mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.nfc.action.tag_discovered" /> <category android:name="android.intent.category.default" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
now, want intent show toast without starting activity again. realize working because when activity restarts, intent gets handled , show me uid.
i want incoming intent show toast containing uid.
sorry if asked before, tried finding no luck.
thanks!
android android-intent nfc mifare nexus-5
Comments
Post a Comment