Android联系人数据库全解析-1.6以下

转自:http://blog.csdn.net/yuzhu2008/article/details/6227246

Android 1.6以及以前的版本API 

本节的开头跟上一节的开头很相似。本节所要介绍的内容将是独立的介绍1.6以及以前版本的API 

权限的授予 

在操作联系人记录之前,你必须在AndroidManifest.xml中声明权限。这样你才能被授权查看联系人。增加下述权限: 

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

查询联系人数据库 

查询联系人详细信息 

为了规范信息,基本的联系人信息放在联系人表里,而其他的详细信息则被储存在独立的数据表中。在Android1.x中,要查询基本的联系人记录,URI被指定在People.CONTENT_URI变量中。

Java代码   收藏代码
  1. package  higherpass.TestContacts;  
  2.   
  3. import  android.app.Activity;  
  4. import  android.content.ContentResolver;  
  5. import  android.database.Cursor;  
  6. import  android.os.Bundle;  
  7. import  android.provider.Contacts;  
  8. import  android.provider.Contacts.People;  
  9.   
  10. public   class  TestContacts  extends  Activity {  
  11.     /** Called when the activity is first created. */   
  12.     @Override   
  13.     public   void  onCreate(Bundle savedInstanceState) {  
  14.         super .onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         ContentResolver cr = getContentResolver();  
  17.         Cursor cur = cr.query(People.CONTENT_URI,   
  18.             null  null  null  null );  
  19.         if  (cur.getCount() >  0 ) {  
  20.          while  (cur.moveToNext()) {  
  21.              String id = cur.getString(cur.getColumnIndex(People._ID));  
  22.              String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));  
  23.          }  
  24.         }  
  25.     }  
  26. }  




这段代码从试图加载中启动后,我们建立了一个ContentResolver实例来查询储存了联系人的SQLite数据库。这个 ContentResolver查询返回了一个包含了联系人记录的Cursor实例。分别把ID和DISPLAY_NAME存入字符串 id与name中。更多信息,请参照android cursor指导 

手机号码 

手机号码保存在它们自己的表里并且需要进行独立的查询。想要查询手机号码,要用到SDK变量Contacts.Phones.CONTENT_URI。用一个WHERE条件去获得指定联系人的号码

Java代码   收藏代码
  1. if  (Integer.parseInt(cur.getString(  
  2.            cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0 ) {  
  3.     Cursor pCur = cr.query(  
  4.             Contacts.Phones.CONTENT_URI,   
  5.             null ,   
  6.             Contacts.Phones.PERSON_ID +" = ?" ,   
  7.             new  String[]{id},  null );  
  8.     int  i= 0 ;  
  9.     int  pCount = pCur.getCount();  
  10.     String[] phoneNum = new  String[pCount];  
  11.     String[] phoneType = new  String[pCount];  
  12.     while  (pCur.moveToNext()) {  
  13.         phoneNum[i] = pCur.getString(  
  14.                               pCur.getColumnIndex(Contacts.Phones.NUMBER));  
  15.         phoneType[i] = pCur.getString(  
  16.                               pCur.getColumnIndex(Contacts.Phones.TYPE));  
  17.         i++;  
  18.     }   
  19. }  


查询手机表并得到存储与pCur中的Cursor。因为联系人数据库每个联系人可以有多个号码,所以我们要循环遍历结果。除了手机好码之外,我们还会得到手机号码的类型(家庭,工作,移动手机,等等)。 


邮件地址 

查询邮件地址跟查询手机号码是类似的。要建立一个额外的查询从数据库获取邮件地址。查询邮件地址要用到这个URI:Contacts.ContactMethods.CONTENT_EMAIL_URI

Java代码   收藏代码
  1. Cursor emailCur = cr.query(   
  2.         Contacts.ContactMethods.CONTENT_EMAIL_URI,   
  3.         null ,  
  4.         Contacts.ContactMethods.PERSON_ID + " = ?" ,   
  5.         new  String[]{id},  null );   
  6. while  (emailCur.moveToNext()) {   
  7.     // This would allow you get several email addresses   
  8. }   
  9. emailCur.close();  


简单查询Contacts.ContactMethods.CONTENT_EMAIL_URI用一个条件限制,即联系人记录的ID要在 Contacts.ContactMethods.PERSON_ID中。就像手机号码一样,联系人也可以包含多个邮件地址。所以我们仍然要循环遍历数据 记录。 


备注 

自定义的备注被附加在每个联系人的记录中。备注既包含在主联系人记录中也可以简单的从People.NOTES的数据中读出。

Java代码   收藏代码
  1. cur.getString(cur.getColumnIndex(People.NOTES));  



邮政地址 

Android允许给联系人存储多个邮政地址。地址存储在联系人方法表中,我们需要加上附加的条件来查询数据。增加一个 Contacts.ContactMethods.KIND为 Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的条件可以仅仅从 Contacts.ContactMethods.CONTENT_URI这个表中取出邮件地址

Java代码   收藏代码
  1. String addrWhere = Contacts.ContactMethods.PERSON_ID   
  2.                + " = ? AND "  + Contacts.ContactMethods.KIND +  " = ?" ;   
  3. String[] addrWhereParams = new  String[]{id,   
  4.     Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};        
  5. Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,   
  6.                null , addrWhere, addrWhereParams,  null );   
  7. while (addrCur.moveToNext()) {  
  8.     String addr = addrCur.getString(  
  9.                   addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));  
  10.     String type = addrCur.getString(  
  11.                   addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));  
  12. }   
  13. addrCur.close();  


查询联系人方法表,外加两个条件从句,一个限制联系人ID,一个指定Contacts.ContactMethods.KIND与 Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE对应以确保查询的为邮件地址。Andoird可以给 联系人存储多个邮件地址。所以要循环遍历返回的结果。Android还存储地址的类型。在1.6以及以前的版本,地址被存储为包含信息的随意的字符串。在 2.0以后,地址被分割为地址的各个部分。 

即时消息(IM) 

即时消息查询查询工作跟之前的两个差不多。数据从Contacts.ContactMethods.CONTENT_URI中查出并附加ID和 Contacts.ContactMethods.KIND 为Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE的条件从句。

Java代码   收藏代码
  1. String imWhere = Contacts.ContactMethods.PERSON_ID   
  2.               + " = ? AND "  + Contacts.ContactMethods.KIND +  " = ?" ;   
  3. String[] imWhereParams = new  String[]{id,   
  4.     Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};   
  5. Cursor imCur = cr.query(Contacts.ContactMethods.CONTENT_URI,   
  6.                null , imWhere, imWhereParams,  null );   
  7. if  (imCur.moveToFirst()) {   
  8.     String imName = imCur.getString(  
  9.                    imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));  
  10.     String imType = imCur.getString(  
  11.                    imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));  
  12. }   
  13. imCur.close();  



组织机构 

联系人的最后一个要讲述的部分是组织数据。Android联系人包含了职务,专业,社会关系和角色以及标题。这些记录可以通过URI:Contacts.Organizations.CONTENT_URI来查询。

Java代码   收藏代码
  1. String orgWhere = Contacts.ContactMethods.PERSON_ID +  " = ?" ;   
  2. String[] orgWhereParams = new  String[]{id};   
  3. Cursor orgCur = cr.query(Contacts.Organizations.CONTENT_URI,   
  4.              null , orgWhere, orgWhereParams,  null );  
  5. if  (orgCur.moveToFirst()) {   
  6.     String orgName = orgCur.getString(  
  7.                   orgCur.getColumnIndex(Contacts.Organizations.COMPANY));  
  8.     String title = orgCur.getString(  
  9.                   orgCur.getColumnIndex(Contacts.Organizations.TITLE));  
  10. }   
  11. orgCur.close();  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值