Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47374415
Some Android phones will have a number of commonly used features, such as ordering telephone, public telephone, air tickets, and so on, and so on, click on the corresponding number will automatically pop up the phone call interface. Well, let's get down to it. A small example of a cell phone's usual number function is implemented.
First, the principle
First of all, these common numbers are placed in a SQLite database, which we read from the database, in the order shown in the Android system provided by the Expandablelistview ( For detailed usage of Expandablelistview please refer to the official Android documentation, I will not say it here), then set a click event for each entry, activate the phone call interface in the Click event, and pass the corresponding number to the call interface.
Well, the principle is finished, is it very simple? Below, let's go through these specific functions together.
Ii. implementation of 1, preparing the database
We first place the database in the assets directory under the Android project
Such as:
2, the operation of creating data class Commonnumberservice
New data operation class Commonnumberservice, there are 4 main methods in this class, one is the construction method, in the construction method we pass the Android context object to this class, the other 3 methods implemented to copy the database to the/data/data/application package name/ Files directory, and then implement query operations for the data.
1) Implementation of construction method
In the construction method, we pass the context object to this class
The specific implementation code is as follows:
Private context Context;public Commonnumberservice (context context) {super (); this.context = context;}
2) Get the common Number group data method
The specific code is as follows:
3) Get common number sub-entry data
The specific code is as follows:
/** * Get data for sub-entries * @return */public list<list<map<string, string>>> getchilddata () {List<list<map <string, string>>> childdata = new arraylist<list<map<string,string>>> (); list<map<string, string>> groupdata = This.getgroupdata (); File File = new file (Context.getfilesdir (), "commonnum.db"); Sqlitedatabase db = Sqlitedatabase.opendatabase (File.getabsolutepath (), NULL, sqlitedatabase.open_readonly); Db.isopen ()) {for (int i = 0; i < groupdata.size (); i++) {String idx = groupdata.get (i). Get ("idx"); list<map<string, string>> list = new arraylist<map<string,string>> (); Cursor C = db.query ("table" + idx, new string[]{"_id", "number", "name"}, NULL, NULL, NULL, NULL, NULL); while (C.movetonext ()) {map<string, string> Map = new hashmap<string, string> (); String name = c.getstring (C.getcolumnindex ("name")); String number = c.getstring (C.getcolumnindex ("number")), Map.put ("name", name), Map.put ("number", number); List.add (map);} C.close (); Childdata.add (list);} Db.close ();} return childdata;}
4) Overall Code
The specific implementation code is as follows:
Package Cn.lyz.mobilesafe.engine;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Java.io.outputstream;import java.util.arraylist;import Java.util.hashmap;import Java.util.List;import Java.util.map;import Android.content.context;import Android.database.cursor;import android.database.sqlite.sqlitedatabase;/** * Get Common numbers * @author Liuyazhuang * */public class Commonnumberservice {private C Ontext Context;public Commonnumberservice (context context) {super (); this.context = context;} /** * Copy the data class to the files directory */public void Copydbtofilesdir () {//Copy the database to the files directory file File = new file (Context.getfilesdir (), " Commonnum.db "), if (!file.exists ()) {try {InputStream in = Context.getassets (). Open (" Commonnum.db "), outputstream out = New FileOutputStream (file); byte[] buffer = new Byte[1024];int len = 0;while (len = in.read (buffer))! =-1) {Out.write (Buffe R, 0, Len);} Out.close (); In.close ();} catch (Exception e) {e.printstacktrace ();}}} /** * Get Group Data * @return */public LIST<MAP<STRING, string>> getgroupdata () {list<map<string, string>> groupdata = new arraylist<map<string, String>> (); File File = new file (Context.getfilesdir (), "commonnum.db"); Sqlitedatabase db = Sqlitedatabase.opendatabase (File.getabsolutepath (), NULL, sqlitedatabase.open_readonly); Db.isopen ()) {Cursor c = db.query ("Classlist", New string[]{"name", "IDX"}, NULL, NULL, NULL, NULL, NULL); while (C.movetone XT ()) {map<string, string> Map = new hashmap<string, string> (); String name = c.getstring (C.getcolumnindex ("name")); String idx = c.getstring (C.getcolumnindex ("idx")); Map.put ("name", name), Map.put ("idx", idx); Groupdata.add (map);} C.close ();d b.close ();} return groupdata;} /** * Get data for sub-entries * @return */public list<list<map<string, string>>> getchilddata () {List<list<map <string, string>>> childdata = new arraylist<list<map<string,string>>> (); list<map<string, string>> groupdata = This.getgroupdata (); File File = new file (Context.getfilesdir (), "commonnum.db"); Sqlitedatabase db = Sqlitedatabase.opendatabase (File.getabsolutepath (), NULL, sqlitedatabase.open_readonly); Db.isopen ()) {for (int i = 0; i < groupdata.size (); i++) {String idx = groupdata.get (i). Get ("idx"); list<map<string, string>> list = new arraylist<map<string,string>> (); Cursor C = db.query ("table" + idx, new string[]{"_id", "number", "name"}, NULL, NULL, NULL, NULL, NULL); while (C.movetonext ()) {map<string, string> Map = new hashmap<string, string> (); String name = c.getstring (C.getcolumnindex ("name")); String number = c.getstring (C.getcolumnindex ("number")), Map.put ("name", name), Map.put ("number", number), List.add ( map);} C.close (); Childdata.add (list);} Db.close ();} return childdata;}}
2, interface display class Commonnumberactivity
In this class we mainly implement, get the interface control, and then call the Commonnumberservice class method, the interface encapsulates the data, while setting the Expandablelistview sub-entries of the Click event, activate the call interface, and pass the selected phone number to the dial-up phone interface.
The specific implementation code is as follows:
Package Cn.lyz.mobilesafe.activity;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Java.io.outputstream;import java.util.list;import Java.util.map;import Android.app.activity;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.View;import Android.widget.expandablelistview;import Android.widget.expandablelistview.onchildclicklistener;import Android.widget.simpleexpandablelistadapter;import Cn.lyz.mobilesafe.r;import cn.lyz.mobilesafe.engine.commonnumberservice;/** * Common number * @author Liuyazhuang * */public class Commonnumberactivity Extends Activity {private Expandablelistview elv_common_number;private commonnumberservice service;private Simpleexpandablelistadapter adapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.common_number); elv_common_ Number = (Expandablelistview) Findviewbyid (R.id.elv_common_number); this.service = new Commonnumberservice (this);//Copy the database to the Files directory Service.copydbtofilesdir ();//Get Group Data list< map<string, string>> groupdata = Service.getgroupdata ();//Get sub-entry data list<list<map<string, String> >> childdata = service.getchilddata () adapter = new Simpleexpandablelistadapter (this, Groupdata, Android. R.layout.simple_expandable_list_item_1, new string[]{"name"}, new int[]{android. R.ID.TEXT1}, Childdata, Android. R.layout.simple_expandable_list_item_2, new string[]{"name", "number"}, new int[]{android. R.id.text1, Android. R.ID.TEXT2}); Elv_common_number.setadapter (adapter); Elv_common_number.setonchildclicklistener (new Myonchildclicklistener ());} /** * Click event * @author Liuyazhuang * */private class Myonchildclicklistener implements onchildclicklistener{@Overridepublic Boolean Onchildclick (expandablelistview parent, View v,int groupposition, int childposition, long id) {map<string, Str ing> map = (map<string, string>) adapter.getchild (Groupposition, Childposition); String number = Map.get ("number"), Intent Intent = new Intent (); intent.setaction (intent.action_dial); Intent.setdata ( Uri.parse ("Tel:" +number); startactivity (intent); return false;}}}
3. Interface Layout Common_number.xml
This layout is simple, which is to place a Expandablelistview
The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <expandablelistview android:id= "@+id/elv_common_number" android: Layout_width= "Match_parent" android:layout_height= "Match_parent"/></linearlayout>
Third, the Operation effect
Four, warm tips
You can go to the link http://download.csdn.net/detail/l1028386804/8980263 download the database commonnum.db file used in the instance
In this example, for the sake of the aspect, I write some text directly in the layout file and the related class, Everyone in the real project to write these words in the String.xml file, in the external reference to these resources, remember, this is the most basic development knowledge and specifications as an Android programmer, I am here just to facilitate directly written in the class and layout files.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android--commonly used mobile phone number function