API For 1.6 and Before 1.6之前的版本

Granting Access 授予权限
<uses-permission android:name="android.permission.READ_CONTACTS" />

Querying the contact database 联系人数据库查询


Retrieving Contact Details 获取联系方式


本的联系人信息存储在联系人表中,而详细信息存储在个人表中。在 Android1.x 中查询的联系人记录数据库的URI是People.CONTENT_URI。

package com.test;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts.People;public class TestContacts extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);               setContentView(R.layout.main);              ContentResolver cr = getContentResolver();               Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);                if(cur.getCount()>0){                     while (cur.moveToNext()) {                             String id = cur.getString(cur.getColumnIndex(People._ID));                             String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));                      }        }         }}


Phone Numbers 电话号码

查询的URI换成Contacts.Phones.CONTENT_URI。
if (cur.getInt(cur.getColumnIndex(People.PRIMARY_PHONE_ID)) > 0) {                            Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,null,                            Contacts.Phones.PERSON_ID +" = ?",new String[]{id}, null);         int i=0;                             int pCount = pCur.getCount();                             String[] phoneNum = new String[pCount];                      String[] phoneType = new String[pCount];                     while (pCur.moveToNext()) {             phoneNum[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));                    phoneType[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE));                                                         i++;                    }    }

以存储多个电话号码,我们需要遍历返回的结果。查询除了返回电话号码外还返回了其他(家庭,工作,手机等)类型。

Email Addresses 邮件地址

Cursor emailCur = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,null,                        Contacts.ContactMethods.PERSON_ID + " = ?",new String[]{id}, null);                   while (emailCur.moveToNext()) {                     // This would allow you get several email addresses                }                     emailCur.close();  


Notes 注释

为每个联系人附加自定义注释。注释存储在联系人记录中,并通过People.NOTES存储的数据进行访问。

String notes=cur.getString(cur.getColumnIndex(People.NOTES));

Postal Addresses 邮政地址
每个联系人都可以存储多个邮政地址。地址存储在联系方式表中,要获取数据,需要有次要条件。添加适配Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的条件Contacts.ContactMethods.KIND来访问Contacts.ContactMethods.CONTENT_URI传递过来的地址。

String addrWhere = Contacts.ContactMethods.PERSON_ID                 + " = ? AND " + Contacts.ContactMethods.KIND + " = ?";                 String[] addrWhereParams = new String[]{id,                     Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};                         Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,                             null, addrWhere, addrWhereParams, null);                 while(addrCur.moveToNext()) {                    String addr = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));                    String type = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));                }                 addrCur.close();

更多相关文章

  1. Android Studio 1.5.1最新版下载地址
  2. Android之根据经纬度查询位置地址名称
  3. android sdk 安装时无法连接到地址
  4. Android Studio 2.0 Preview发布,附下载地址,支持即时运行和GPU P
  5. Android设备获取wifi下的ipv6地址
  6. Android网络游戏之神农诀项目开发--视频观看地址
  7. 最新eclipse中android插件安装下载地址
  8. android手机两种方式获取IP地址

随机推荐

  1. AndroidManifest.xml文件详解(manifest)
  2. 关于android USB Host 串口编程
  3. Android开发9――Activity的启动模式
  4. android 7.0 关机流程详细分析
  5. Android的四种启动模式
  6. 技术转载:Android开发之常用代码片段
  7. android ndk开发中常用的系统自带网络命
  8. android ValueAnimator ObjectAnimator
  9. Android消息机制---Handler工作原理
  10. Android(安卓)架构概况,学习笔记。