手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来。
1)Phone.CONTENT_URI,获取联系人的时候需要去这个url中去找数据 。它所指向的其实是“content:// com.android.contacts/data/phones”。这个url 对应着手机系统的contacts表 和 raw_contacts表 以及 data表 所以说我们的数据都是从这三个表中获取的
/**获取库Phone表字段**/
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
它的意思是只去表中找 显示名称 电话号码 头像ID 联系人ID 这4个数据 ,如果你须要其它数据 的话 就须要修改这里
1.如何获得手机通讯录联系人信息
/**得到手机通讯录联系人信息**/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitamp
Bitmap contactPhoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 ) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
2.任意点击一个联系人会调用系统拨打电话的界面 ,代码如下。
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view,
int position, long id) {
//调用系统方法拨打电话
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
}
});
MainActivity.java
package com.pxd.getcontacts;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
Context mContext = null;
/**获取库Phone表字段**/
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
/**联系人显示名称**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**电话号码**/
private static final int PHONES_NUMBER_INDEX = 1;
/**头像ID**/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**联系人的ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3;
/**联系人名称**/
private ArrayList mContactsName = new ArrayList();
/**联系人头像**/
private ArrayList mContactsNumber = new ArrayList();
/**联系人头像**/
private ArrayList mContactsPhonto = new ArrayList();
ListView mListView = null;
MyListAdapter myAdapter = null;
private Button getInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInfo = (Button) findViewById(R.id.getinfo);
mContext = this;
mListView = this.getListView();
myAdapter = new MyListAdapter(this);
//获取联系人按钮
getInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContactsName.clear();
mContactsNumber.clear();
mContactsPhonto.clear();
setListAdapter(myAdapter);
getPhoneContacts();
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view,
int position, long id) {
//调用系统方法拨打电话
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
}
});
}
/**得到手机通讯录联系人信息**/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitamp
Bitmap contactPhoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 ) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
class MyListAdapter extends BaseAdapter {
public MyListAdapter(Context context) {
mContext = context;
}
public int getCount() {
//设置绘制数量
return mContactsName.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null || position < mContactsNumber.size()) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.item, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
}
//绘制联系人名称
title.setText(mContactsName.get(position));
//绘制联系人号码
text.setText(mContactsNumber.get(position));
//绘制联系人头像
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
}
}
}
Activity_main.xml
item.xml