
  Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来。 这一篇文章我主要带领同学们熟悉Android的通讯录机制。
  图中选中的数据库 contacts2.db就是系统储存联系人的数据库,我们将它打开看看里面储存了些什么东东?

  打开contacts.db后 发面里面有一堆表,同学们先别慌张。今天我们主要讨论红框内的4个比较常用的表,后期我在介绍其它表的使用。这里说一下如果你想在真机上查看数据库的话必需要先获得root权限,否则无法查看。

  1.contacts 表
  _id :表的ID,主要用于其它表通过contacts 表中的ID可以查到相应的数据。
  display_name:  联系人名称
  photo_id:头像的ID,如果没有设置联系人头像,这个字段就为空
  times_contacted:通话记录的次数
  last_time_contacted: 最后的通话时间
  lookup :是一个持久化的储存 因为用户可能会改名子 但是它改不了lookup

  2.data表
  raw_contact_id:通过raw_contact_id可以找到 raw_contact表中相对的数据。
  data1 到 data15  这里保存着联系人的信息 联系人名称 联系人电话号码  电子邮件  备注 等等。

  3.phone_look_up表
  data_id  : 通过data_id可以找到 datat表中相对的数据。
  raw_contact_id : 通过raw_contact_id 可以找到 raw_contact_表中相对的数据。
  normalized_number: 这个字段就比较有意思了,它是将每个电话号码逆序排列。

  4.raw_contact表
  version :版本号,用于监听变化
  deleted :删除标志, 0为默认 1 表示这行数据已经删除
  display_name : 联系人名称
  last_time_contacts : 最后联系的时间

  有关这些的源码都在android.provider.ContactsContract这个类里面,如果想深入了解的话 可以去看看,数据库相关的操作 联查啊 啥的  都在里面。
  下面说说代码是怎么用的
  先说说 Phone.CONTENT_URI,获取联系人的时候需要去这个url中去找数据 。它所指向的其实是“content:// com.android.contacts/data/phones”。这个url 对应着contacts表 和   raw_contacts表 以及 data表 所以说我们的数据都是从这三个表中获取的。
  这里强调一下query第二个参数
Java代码
    - private static final String[] PHONES_PROJECTION = new String[] {  
-         Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };  
  它的意思是只去表中找 显示名称  电话号码 头像ID  联系人ID 这4个数据 ,如果你须要其它数据 的话 就须要修改这里。
  获得手机通讯录联系人信息
Java代码
    -   
- 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);  
-    
-           
-         Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);  
-    
-           
-         Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);  
-    
-           
-         Bitmap contactPhoto = null;  
-    
-           
-         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();  
-     }  
- }
  获得手机sim卡联系人信息
  sim卡和手机本人 获取的方式类似 只是url有点不一样 ,须要注意的一点是 sim卡  是没有联系人头像的。
Java代码
    -   
- private void getSIMContacts() {  
-     ContentResolver resolver = mContext.getContentResolver();  
-       
-     Uri uri = Uri.parse("content://icc/adn");  
-     Cursor phoneCursor = resolver.query(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);  
-    
-           
-    
-         mContactsName.add(contactName);  
-         mContactsNumber.add(phoneNumber);  
-         }  
-    
-         phoneCursor.close();  
-     }  
- }
  这个界面就可以看到联系人的 名称 号码 以及头像了。如果想在模拟器上看须要将图片拷贝到SD卡中,然后在联系人中设置一下,这里就可以看到头像了,或者 真机上会比较清楚、

  任意点击一个联系人会调用系统拨打电话的界面 ,代码如下。 
Java代码
    -   
- Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri  
-        .parse("tel:" + mContactsNumber.get(position)));  
- startActivity(dialIntent);  

XML/HTML代码
    -   
- <uses-permission android:name="android.permission.READ_CONTACTS"/>  
-   
- <uses-permission android:name="android.permission.CALL_PHONE"/>  
  下面给出完整的代码
Java代码
    - import java.io.InputStream;  
- import java.util.ArrayList;  
-    
- 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.net.Uri;  
- import android.os.Bundle;  
- import android.provider.ContactsContract;  
- import android.provider.ContactsContract.CommonDataKinds.Phone;  
- import android.provider.ContactsContract.CommonDataKinds.Photo;  
- 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.ImageView;  
- import android.widget.ListView;  
- import android.widget.TextView;  
- import android.widget.AdapterView.OnItemClickListener;  
-    
- public class ContactsActivity extends ListActivity {  
-    
-     Context mContext = null;  
-    
-       
-     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;  
-    
-       
-     private static final int PHONES_PHOTO_ID_INDEX = 2;  
-    
-       
-     private static final int PHONES_CONTACT_ID_INDEX = 3;  
-    
-       
-     private ArrayList<String> mContactsName = new ArrayList<String>();  
-    
-       
-     private ArrayList<String> mContactsNumber = new ArrayList<String>();  
-    
-       
-     private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();  
-    
-     ListView mListView = null;  
-     MyListAdapter myAdapter = null;  
-    
-     @Override  
-     public void onCreate(Bundle savedInstanceState) {  
-     mContext = this;  
-     mListView = this.getListView();  
-       
-     getPhoneContacts();  
-    
-     myAdapter = new MyListAdapter(this);  
-     setListAdapter(myAdapter);  
-    
-     mListView.setOnItemClickListener(new 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);  
-         }  
-     });  
-    
-     super.onCreate(savedInstanceState);  
-     }  
-    
-       
-     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);  
-    
-           
-         Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);  
-    
-           
-         Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);  
-    
-           
-         Bitmap contactPhoto = null;  
-    
-           
-         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();  
-     }  
-     }  
-    
-       
-     private void getSIMContacts() {  
-     ContentResolver resolver = mContext.getContentResolver();  
-       
-     Uri uri = Uri.parse("content://icc/adn");  
-     Cursor phoneCursor = resolver.query(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);  
-    
-           
-    
-         mContactsName.add(contactName);  
-         mContactsNumber.add(phoneNumber);  
-         }  
-    
-         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.colorlist, 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;  
-     }  
-    
-     }  
- }  
XML/HTML代码
    - <?xml version="1.0" encoding="utf-8"?>    
-    
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
-     android:layout_width="fill_parent" android:layout_height="wrap_content">  
-     <ImageView android:id="@+id/color_image"  
-         android:layout_width="40dip" android:layout_height="40dip" />  
-     <TextView android:id="@+id/color_title"  
-         android:layout_width="fill_parent" android:layout_height="wrap_content"  
-         android:layout_toRightOf="@+id/color_image"  
-         android:layout_alignParentBottom="true"  
-         android:layout_alignParentRight="true" android:singleLine="true"  
-         android:ellipsize="marquee"  
-         android:textSize="15dip"  />  
-     <TextView android:id="@+id/color_text"  
-         android:layout_width="fill_parent" android:layout_height="wrap_content"  
-         android:layout_toRightOf="@+id/color_image"  
-         android:layout_below="@+id/color_title"  
-         android:layout_alignParentBottom="true"  
-         android:layout_alignParentRight="true"  
-         android:singleLine="true"  
-         android:ellipsize="marquee"  
-         android:textSize="20dip" />  
- </RelativeLayout>  
  下载地址:http://vdisk.weibo.com/s/aa4uT