Android--Frequent use of mobile phone number function

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47374415

Some Android phones have some features that often use numbers, such as ordering phone calls, public telephones, airline tickets, and so on, and clicking on the corresponding number will take the initiative to pop up the phone call interface.

Well, let's do this together. A sample of a phone's regular use of the number function is implemented.

First, the principle

First of all. These frequently used numbers are placed in a SQLite database, which we read from the database. In accordance with the corresponding sequence shown to the Android system provided by the Expandablelistview (about the specific use of Expandablelistview please refer to the official Android document, I do not say more here), Then set a click event for each entry. Activates the phone call interface in the Click event. and pass the corresponding number to the call interface.

OK, the principle is finished, is not very easy? Below, let's implement these detailed functions together.

Ii. implementation of 1, preparing the database

We first place the database in the Assets folder under the Android project

For example, with:

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, and in the construction method we pass the Android context object to this class. The other 3 methods implement the Copy database to the/data/data/app package name/files folder. Then implement the query operation of the data.

1) Implementation of construction method

In the constructor method. We pass the context object to this class

Detailed implementation code such as the following:

Private context Context;public Commonnumberservice (context context) {super (); this.context = context;}

2) Get the regular use of the number group data method

Detailed code such as the following:

3) Get frequently used number sub-entry data

Detailed code such as the following:

/** * 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

Detailed implementation code such as the following:

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 regular use number * @author Liuyazhuang * */public class Commonnumberservice { Private context Context;public Commonnumberservice (context context) {super (); this.context = context;} /** * Copy the data class to the Files folder */public void Copydbtofilesdir () {//Copy the database to the files folder 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 are mainly implemented. Gets the interface control, then calls the method in the Commonnumberservice class, encapsulates the data to the interface, sets the Click event of the Expandablelistview subkey at the same time, activates the call interface, and passes the selected phone number to the dial-up interface.


Detailed implementation code such as the following:

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;/** * Frequently use 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 Database to Files folder Service.copydbtofilesdir ();//Get Group Data list <map<string, string>> groupdata = Service.getgroupdata ();//Get Child 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 very easy, is to put a Expandablelistview

Detailed implementations such as the following:

<?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>

Iii. Effect of implementation


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, external reference these resources. Remember, this is the most important development knowledge and specification of an ape as an Android program, and I am here just to facilitate writing directly in the class and layout files.

Android--Frequent use of mobile phone number function

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.