First, the result of the operation
Since the contacts are stored in a database on the phone, we can use a method
Context.getcontentresolver (). query (phone.content_uri,null, NULL, NULL, NULL);
To get the address book, this method returns the data type of a cursor, using the MoveToNext () method to get all the phone number information
Of course, reading the phone Address Book requires permission to be declared in the Adnroidmanifest file
Because I also realized the function of calling, so I also want to declare permission
<uses-permission android:name= "Android.permission.READ_CONTACTS"/><uses-permission android:name= " Android.permission.CALL_PHONE "/>
Layout file
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <listview android:id= "@+id/listview1" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" > </ListView></RelativeLayout>
ListView Layout File: Item.xml, where I set the Avatar as the default of course also can read the contact's icon in the phone database
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <imageview Andro Id:id= "@+id/image" android:layout_width= "60DP" android:layout_height= "60DP" android:padding= "10DP" android:src= "@drawable/ic_launcher"/> <textview android:id= "@+id/name" android:paddingtop= "10 DP "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "android:layout_torightof= "@id/image" android:text= "name"/> <textview android:id= "@+id/number" android:paddingtop= "5d P "android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:layout_below=" @id/ Name "android:layout_torightof=" @id/image "android:text=" number "/></relativelayout>
A class that encapsulates a contact information by itself has two variables
Package Com.example.getphonenumber;public class Phoneinfo {private string Name;private string Number;public phoneinfo ( String name, string number) {this.name = Name;this.number = number;} Public String GetName () {return name;} Public String GetNumber () {return number;}}
Read contacts in the phone database
Getphonenumberfrommobile.class
Package Com.example.getphonenumber;import Java.util.arraylist;import Java.util.list;import android.content.Context ; Import Android.database.cursor;import Android.provider.contactscontract.commondatakinds.phone;public class Getphonenumberfrommobile {private list<phoneinfo> list;public list<phoneinfo> GetPhoneNumberFromMobile ( Context context) {//TODO auto-generated constructor stublist = new Arraylist<phoneinfo> (); cursor cursor = context.getcontentresolver (). query (phone.content_uri,null, NULL, NULL, NULL);// The MoveToNext method returns a Boolean type of data while (Cursor.movetonext ()) {//reads the name of the address Book string name = Cursor.getstring ( Cursor.getcolumnindex (Phone.display_name));//The number of the Address book is read string # = Cursor.getstring (Cursor.getcolumnindex ( Phone.number)); Phoneinfo phoneinfo = new Phoneinfo (name, number); List.add (phoneinfo);} return list;}}
Custom Adapter
Package Com.example.getphonenumber;import Java.util.arraylist;import Java.util.list;import android.content.Context ; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Myadapter extends Baseadapter{private List <PhoneInfo> list;private Context context;public myadapter (list<phoneinfo> list, context context) { This.list = List;this.context = Context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn list.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn list.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubif (Conve Rtview==null) {Viewholder viewholder=new viewholder (); Layoutinflater Inflater=layoutinflater.from (context); converTview=inflater.inflate (R.layout.item, null); Viewholder.name= (TextView) Convertview.findviewbyid (R.id.name); Viewholder.number= (TextView) Convertview.findviewbyid (R.id.number); ViewHolder.name.setText (List.get (position). GetName ()); ViewHolder.number.setText (List.get (position). GetNumber ());} return Convertview;} public class Viewholder{textview name; TextView number;}}
Mainactivity in the ListView load adapter and add a click Listener event for it
Package Com.example.getphonenumber;import Java.util.arraylist;import Java.util.list;import android.net.Uri;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.Menu;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Activity implements Onitemclicklistener {private ListView lv;private myadapter adapter;private getphonenumberfrommobile Getphonenumberfrommobile;private list<phoneinfo> List = new arraylist<phoneinfo> ();p rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LV = (ListView) Findviewbyid (r.id.listview1); getphonenumberfrommobile = new Getphonenumberfrommobile (); list = Getphonenumberfrommobile.getphonenumberfrommobile (This): adapter = new Myadapter (list, this); Lv.setadapter (adapter) ; LV.SETONITEMCLicklistener (this);} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {//TODO auto-generated met Hod stubstring number = List.get (position). GetNumber (); Intent Intent = new Intent (); Intent.setaction (" Android.intent.action.CALL "); Intent.addcategory (Intent.category_default); Intent.setdata (Uri.parse (" Tel: "+ number)); StartActivity (intent);}}
Android: Get your phone number and name for your address book