Android联系人数据库全解析(2)
Android 1.6以及以前的版本API
本节的开头跟上一节的开头很相似。本节所要介绍的内容将是独立的介绍1.6以及以前版本的API
权限的授予
在操作联系人记录之前,你必须在AndroidManifest.xml中声明权限。这样你才能被授权查看联系人。增加下述权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
查询联系人数据库
查询联系人详细信息
为了规范信息,基本的联系人信息放在联系人表里,而其他的详细信息则被储存在独立的数据表中。在Android1.x中,要查询基本的联系人记录,URI被指定在People.CONTENT_URI变量中。
- package higherpass.TestContacts;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.Contacts;
- import android.provider.Contacts.People;
- public class TestContacts extends Activity {
- /** Called when the activity is first created. */
- @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));
- }
- }
- }
- }
这段代码从试图加载中启动后,我们建立了一个ContentResolver实例来查询储存了联系人的SQLite数据库。这个 ContentResolver查询返回了一个包含了联系人记录的Cursor实例。分别把ID和DISPLAY_NAME存入字符串 id与name中。更多信息,请参照android cursor指导
手机号码
手机号码保存在它们自己的表里并且需要进行独立的查询。想要查询手机号码,要用到SDK变量Contacts.Phones.CONTENT_URI。用一个WHERE条件去获得指定联系人的号码
- if (Integer.parseInt(cur.getString(
- 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++;
- }
- }
查询手机表并得到存储与pCur中的Cursor。因为联系人数据库每个联系人可以有多个号码,所以我们要循环遍历结果。除了手机好码之外,我们还会得到手机号码的类型(家庭,工作,移动手机,等等)。
邮件地址
查询邮件地址跟查询手机号码是类似的。要建立一个额外的查询从数据库获取邮件地址。查询邮件地址要用到这个URI:Contacts.ContactMethods.CONTENT_EMAIL_URI
- 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();
简单查询Contacts.ContactMethods.CONTENT_EMAIL_URI用一个条件限制,即联系人记录的ID要在 Contacts.ContactMethods.PERSON_ID中。就像手机号码一样,联系人也可以包含多个邮件地址。所以我们仍然要循环遍历数据 记录。
备注
自定义的备注被附加在每个联系人的记录中。备注既包含在主联系人记录中也可以简单的从People.NOTES的数据中读出。
- cur.getString(cur.getColumnIndex(People.NOTES));
邮政地址
Android允许给联系人存储多个邮政地址。地址存储在联系人方法表中,我们需要加上附加的条件来查询数据。增加一个 Contacts.ContactMethods.KIND为 Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的条件可以仅仅从 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();
查询联系人方法表,外加两个条件从句,一个限制联系人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的条件从句。
- String imWhere = Contacts.ContactMethods.PERSON_ID
- + " = ? AND " + Contacts.ContactMethods.KIND + " = ?" ;
- String[] imWhereParams = new String[]{id,
- Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};
- Cursor imCur = cr.query(Contacts.ContactMethods.CONTENT_URI,
- null , imWhere, imWhereParams, null );
- if (imCur.moveToFirst()) {
- String imName = imCur.getString(
- imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
- String imType = imCur.getString(
- imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
- }
- imCur.close();
组织机构
联系人的最后一个要讲述的部分是组织数据。Android联系人包含了职务,专业,社会关系和角色以及标题。这些记录可以通过URI:Contacts.Organizations.CONTENT_URI来查询。
- String orgWhere = Contacts.ContactMethods.PERSON_ID + " = ?" ;
- String[] orgWhereParams = new String[]{id};
- Cursor orgCur = cr.query(Contacts.Organizations.CONTENT_URI,
- null , orgWhere, orgWhereParams, null );
- if (orgCur.moveToFirst()) {
- String orgName = orgCur.getString(
- orgCur.getColumnIndex(Contacts.Organizations.COMPANY));
- String title = orgCur.getString(
- orgCur.getColumnIndex(Contacts.Organizations.TITLE));
- }
- orgCur.close();