Android: Address Book for obtaining mobile phone numbers and names
First, the running result
Since the address book is stored in a database on the mobile phone, we can use one method.
context.getContentResolver().query(Phone.CONTENT_URI,null, null, null, null);
This method returns the Data Type of a cursor. The moveToNext () method is used to obtain information about all mobile phone numbers.
Of course, the permission to read the mobile phone address book must be declared in the adnroidManifest file.
Because I have also implemented the call function, I also need 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>
The layout file of listview: item. xml. The Avatar I set here is the default one. Of course, you can also read the icon of the contact in the mobile phone 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>
The 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 the address book in the mobile 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
List; public List
GetPhoneNumberFromMobile (Context context) {// TODO Auto-generated constructor stublist = new ArrayList
(); Cursor cursor = context. getContentResolver (). query (Phone. CONTENT_URI, null, null); // The moveToNext method returns a boolean type of data while (cursor. moveToNext () {// read the address book name String name = cursor. getString (cursor. getColumnIndex (Phone. DISPLAY_NAME); // read the address book number String number = 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
list;private Context context;public MyAdapter(List
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(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;}}
In MainActivity, The listview loads the adapter and adds a click listening 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
list = new ArrayList
();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);}@Overridepublic void onItemClick(AdapterView
parent, View view, int position,long id) {// TODO Auto-generated method 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);}}