First show you the operating effect chart:
Because the address book is stored in a database on the phone, so 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, through the MoveToNext () method to obtain all the mobile phone number information
Of course, read the phone Address Book requires permission to declare in the Adnroidmanifest file
Because I also realized the function of the phone, so I also want to declare permissions
<uses-permission android:name= "Android.permission.READ_CONTACTS" >
<uses-permission android:name= " Android.permission.CALL_PHONE "></uses-permission></uses-permission>
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 you can also read the icon of the contact in the mobile database
<!--{cke_protected}{c}%3c!%2d%2d%3fxml%20version%3d%221.0%22%20encoding%3d% 22utf-8%22%3f%2d%2d%3e--> <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Match_parent" android:layout_height= "match_parent" > <imageview android: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=" 10DP "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= "5DP" android:layout_width= "fill_parent" android:layout_height= "WRAP_" Content "android:layout_below=" @id/name "android:layout_torightof=" @id/image "android:text=" number "> </ Textview></textview></imageview></relativelayout>
A class that encapsulates a contact information of its own 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 the Address book in the Mobile 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 stub list = 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 ()) {//Read the name of the address Book String name = cursor.getstring (cursor.
Getcolumnindex (Phone.display_name));
Read the number of the address Book String = cursor.getstring (cursor. Getcolumnindex (phone.number));
Phoneinfo phoneinfo = new Phoneinfo (name, number);
List.add (Phoneinfo);
} return list; }}</phoneinfo></phoneinfo></phoneinfo>
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, public myadapter ( List<phoneinfo> list, Context context) {this.list = list; this.context = context;} @Override public int getcount () {//Todo auto-generated method stub return List.size (): @Override public Object getitem (int position) {//Todo auto-g
enerated Method Stub return List.get (position); @Override public long getitemid (int position) {//TODO auto-generated method stub return position;} @Override Public V
Iew GetView (int position, View Convertview, ViewGroup parent) {//TODO auto-generated Method stub if (convertview==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; }}</phoneinfo></phoneinfo>
mainactivity ListView Load the adapter and add a hit 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> (); protected 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); @Override public void Onitemclick (adapterview<!--?--> parent, view view, int position, long ID) {//TODO Auto-gen
erated Method Stub String 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); }}</phoneinfo></phoneinfo>