利用Content provide轻松的获取电话联系人,并显示在listview,如下代码:
ackage com.example.contentprovidetest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
public class MainActivity extends Activity {
//定义列表视图
ListView listView;
//定义 adapter
ArrayAdapter<String> adapter;
//定义列表视图
ListView listView;
//定义 adapter
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
//创建Content provide对象
ContentResolver cr = getContentResolver();
//定义通讯录查询集
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
// 新建数组,长度为电话联系人个数
String[] arstr = new String[cursor.getCount()];
//临时变量
int j = 0;
//遍历查询结果集
while (cursor.moveToNext()) {
//获取结果集DISPLAY_NAME列的列索引
int nameColumIndex = cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameColumIndex);
//获取DISPLAY_NAME列的值
// str=str+name;
String str = name;
//根据联系人的ID(唯一),查询此人有多少号码
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);
//遍历电话表的结果集
while (phone.moveToNext()) {
//获取电话号码
String strPhoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//获取号码类型
String phTypes = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
int i = Integer.parseInt(phTypes);
//根据类型确定显示字段
switch (i) {
case 1:
phTypes = "home";
break;
case 2:
phTypes = "mobile";
break;
case 3:
phTypes = "work";
break;
default:
phTypes = "ohter";
break;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
//创建Content provide对象
ContentResolver cr = getContentResolver();
//定义通讯录查询集
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
// 新建数组,长度为电话联系人个数
String[] arstr = new String[cursor.getCount()];
//临时变量
int j = 0;
//遍历查询结果集
while (cursor.moveToNext()) {
//获取结果集DISPLAY_NAME列的列索引
int nameColumIndex = cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameColumIndex);
//获取DISPLAY_NAME列的值
// str=str+name;
String str = name;
//根据联系人的ID(唯一),查询此人有多少号码
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);
//遍历电话表的结果集
while (phone.moveToNext()) {
//获取电话号码
String strPhoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//获取号码类型
String phTypes = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
int i = Integer.parseInt(phTypes);
//根据类型确定显示字段
switch (i) {
case 1:
phTypes = "home";
break;
case 2:
phTypes = "mobile";
break;
case 3:
phTypes = "work";
break;
default:
phTypes = "ohter";
break;
}
//联系人、电话类型、电话
str = str + "\n " + phTypes + ":" + strPhoneNumber;
//联系人、电话类型、电话
str = str + "\n " + phTypes + ":" + strPhoneNumber;
}
//以一个人的联系人、电话类型、电话为一个字符串,保存到数组
arstr[j] = str + "\n";
j++;
//关闭电话集
phone.close();
}
//关闭查询集
cursor.close();
//由数组建立list目录
List list = new ArrayList<>(Arrays.asList(arstr));
//实例化adapter
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
listView = new ListView(this);
// 将adapter适配器置入ListView
listView.setAdapter(adapter);
//以一个人的联系人、电话类型、电话为一个字符串,保存到数组
arstr[j] = str + "\n";
j++;
//关闭电话集
phone.close();
}
//关闭查询集
cursor.close();
//由数组建立list目录
List list = new ArrayList<>(Arrays.asList(arstr));
//实例化adapter
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
listView = new ListView(this);
// 将adapter适配器置入ListView
listView.setAdapter(adapter);
// 设置默认布局为ListView
setContentView(listView);
setContentView(listView);
}
}
编译后运行,你可看到你的所有联系人及电话。建议放在线程读取后再显示更好