NFC-enabled Android Auto-run Program

Source: Internet
Author: User

Recently learning NFC, referring to two good articles

Reference Address:

NFC Basic http://blog.csdn.net/zoeice/article/details/9714867

NFC Advanced http://blog.csdn.net/zoeice/article/details/9714963

This article is the use of NFC label paper and Xiaomi 3 mobile phone test

First create a new activity class

Package Com.cayden.run.application;import Android.app.activity;import Android.app.pendingintent;import Android.content.intent;import Android.nfc.ndefmessage;import Android.nfc.ndefrecord;import Android.nfc.NfcAdapter ; Import Android.nfc.tag;import Android.nfc.tech.ndef;import android.os.bundle;import Android.view.view;import Android.widget.button;import android.widget.toast;/** * * * @author Cuiran * @version 1.0.0 */public class Runapplicati Onactivity extends Activity {private Button mselectautorunapplication;private String mpackagename;private nfcadapter Mnfcadapter;private pendingintent mpendingintent; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_auto_run_ application); mselectautorunapplication = (Button) Findviewbyid (r.id.button_select_auto_run_application); Mnfcadapter = Nfcadapter.getdefaultadapter (this); mpendingintent = pendingintent.getactivity (this, 0, new IntenT (This,getclass ()), 0);} public void Onnewintent (Intent Intent) {if (mpackagename = = null) return; Tag Detectedtag = Intent.getparcelableextra (Nfcadapter.extra_tag); Writenfctag (Detectedtag);} public void Onresume () {super.onresume (); if (mnfcadapter! = null) Mnfcadapter.enableforegrounddispatch (this, Mpendingintent, null,null);}    public void OnPause () {super.onpause (); if (mnfcadapter! = null) Mnfcadapter.disableforegrounddispatch (this);} public void onclick_selectautorunapplication (view view) {Intent Intent = new Intent (This, Installedapplicationlista    Ctivity.class);    Startactivityforresult (Intent, 0); }public void Writenfctag (tag tag) {if (tag = = null) {return;} Ndefmessage ndefmessage = new Ndefmessage (new ndefrecord[] {Ndefrecord.createapplicationrecord (mPackageName)}); int Size = Ndefmessage.tobytearray (). length;try {Ndef Ndef = ndef.get (tag); if (ndef!=null) {ndef.connect (); if (! Ndef.iswritable ()) {return;} if (Ndef.getmaxsize () < size) {return;} Ndef.writendefmessage (Ndefmessage);Toast.maketext (This, "OK", Toast.length_long). Show ();}}  catch (Exception e) {//Todo:handle Exception}} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = 1) {Mselectautorunapplication.settext (Data.getextras (). getString ("Package_name")); String temp = Mselectautorunapplication.gettext (). toString (); mpackagename = temp.substring (Temp.indexof ("\ n") + 1);}}}

in this activity, you can click the button and jump to another activity, which is used to display the list of apps

Package Com.cayden.run.application;import Java.util.arraylist;import Java.util.list;import Android.app.listactivity;import Android.content.intent;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.os.bundle;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;public class Installedapplicationlistactivity extends Listactivity Implementsonitemclicklistener {private list<string> mpackages = new arraylist<string> (); @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Packagemanager Packagemanager = Getpackagemanager (); list<packageinfo> PackageInfos = packagemanager.getinstalledpackages (packagemanager.get_activities); PackageInfo Packageinfo:packageinfos) {Mpackages.add (PackageInfo.applicationInfo.loadLabel (packagemanager) + "\ n" + Packageinfo.packagename);} Arrayadapter<string> Arrayadapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, Android. R.id.text1,mpackages); Setlistadapter (Arrayadapter); Getlistview (). Setonitemclicklistener (this);} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long id) {Intent Intent = new Intent (); Intent.putextra ("Package_name", mpackages.get (position)); Setresult (1, intent); Finish ();}}

When the app is selected, it is recalled and returned

PackageInfo.applicationInfo.loadLabel (Packagemanager) + "\ n" + packageinfo.packagename
To do this, first put the NFC tag paper in the back of the phone to perform

public void Onnewintent (Intent Intent) {if (mpackagename = = null) return; Tag Detectedtag = Intent.getparcelableextra (Nfcadapter.extra_tag); Writenfctag (Detectedtag);}
if it is not empty, write the selected app to the label paper

Then after exiting the app, placing the label sheet on the back of the phone automatically executes the selected application.

about the Ndefrecord also gave the API on the official website

Http://developer.android.com/reference/android/nfc/package-summary.html

NFC-enabled Android Auto-run Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.