接Android Contacts的使用(一)

API For 1.6 and Before 1.6之前的版本

If you just read the begining of this article the first bit of this page is going to look familiar. This page is designed to act as a standalone guide to working with the contacts API in Android 1.6 and before. 假如你刚刚阅读了该文的开头是否有种时曾相识的感觉。该文主要是用来介绍1.6以下版本联系人的API。

Granting Access 授予权限

Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement. 同Android Contacts的使用(一)

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

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

Retrieving Contact Details 获取联系方式

Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 1.x to query the base contact records the URI to query is stored in People.CONTENT_URI. 基本的联系人信息存储在联系人表中,而详细信息存储在个人表中。在 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));
}
}
}
}

Start off with the standard view loading. Then we create a ContentResolver instance that will be used to query the SQLite database that stores the contacts. The ContentResolver query returns a Cursor instance that holds the contact records queried from the database. Then take the ID field from the contact record and store it in the string id and take the DISPLAY_NAME field and place it in the string name. For more information about cursors see the Android Cursor Tutorial.参考Android Contacts的使用(一)

Phone Numbers 电话号码

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable Contacts.Phones.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact. 同Android Contact的使用(一),查询的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
++ ;
}
}

Query the phones table and get a Cursor stored in pCur. Since the Android contacts database can store multiple phone numbers per contact we need to loop through the returned results. In addition to returning the phone number the query also returned the type of number (home, work, mobile, etc). 查询电话表,返回一个游标pCur。由于Android中每个联系人可以存储多个电话号码,我们需要遍历返回的结果。查询除了返回电话号码外还返回了其他(家庭,工作,手机等)类型。

Email Addresses 邮件地址

Querying email addresses is similar to phone numbers. A special query must be performed to get email addresses from the database. Query the URI stored in Contacts.ContactMethods.CONTENT_EMAIL_URI to query the email addresses.同Android Contact的使用(一),查询的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();

Simple query Contacts.ContactMethods.CONTENT_EMAIL_URI with a conditional limiting the results to numbers that match the ID of the contact record matches the value in the field Contacts.ContactMethods.PERSON_ID. As with phone numbers each contact can contain multiple email addresses so we need to loop through the Cursor records. 查询Contacts.ContactMethods.CONTENT_EMAIL_URI,以联系人记录的ID为条件匹配Contacts.ContactMethods.PERSON_ID的值。正如电话号码每个联系人也可以有多个email地址,同样的我们也需要遍历返回的结果。

Notes 注释

Custom notes can be attached to each contact record. Notes though are stored in the main contact record and are simply accessed through the data stored in People.NOTES. 可以为每个联系人附加自定义注释。注释存储在联系人记录中,并通过People.NOTES存储的数据进行访问。

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


Postal Addresses 邮政地址

Android can store multiple postal addresses per contact. Addresses are stored in the contact methods table and need to have a second conditional added to retrieve the data. Add a conditional Contacts.ContactMethods.KIND that matches Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE to only query postal addresses from Contacts.ContactMethods.CONTENT_URI.每个联系人都可以存储多个邮政地址。地址存储在联系方式表中,要获取数据,需要有次要条件。添加适配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();

Query Contacts.ContactMethods.CONTENT_URI with 2 conditionals, one limiting the contact ID and the second Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE to only query postall addresses. Android can store multiple postal addresses so loop through the list of returned results. Android also stores a type record for the address. In Android 1.6 and before the address is stored as a free-form string containing the data. In Android 2.0 and later this has changed to being a series of fields containing parts of the address. 查询Contacts.ContactMethods.CONTENT_URI需要2个条件,一个是联系人的ID另一个是通过Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE来匹配Contacts.ContactMethods.KIND查询postal地址。Android可以通过循环来遍历返回的结果列表中的多个postal地址,他也存储了地址类型的记录。在Android1.6或更早的版本中,该地址是由一个自由格式的字符串存储,在2.0或更高的版本中,已经更改为多个小地址存储。

Instant Messenger (IM) 即时消息

The instant messenger query works just like the previous 2. The data is queried from Contacts.ContactMethods.CONTENT_URI and needs conditionals for the contact ID and Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE. 即时消息查询类似于postal地址查询,查询这些数据也需要联系人的Id和Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE来匹配Contacts.ContactMethods.KIND为条件。

              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();


Organizations 组织

The last part of the contact record to be covered is the Organizations data. The Android contact record can contain information about Employment, professional, and social memberships as well as roles and titles. These records are queried from the URI stored in Contacts.Organizations.CONTENT_URI. 联系人记录的最后一部分数据组织数据。在Android中联系记录可以包含有关就业、职业信息、社会成员以及角色和职称等信息。这些记录需从Contacts.Organizations.CONTENT_URI存储的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();

<未完待续>

更多相关文章

  1. Android(安卓)Studio 官方最新版下载地址(支持国内下载)
  2. Android(安卓)开源项目分类汇总
  3. Android源码及SDK国内镜像下载
  4. Android的一些网上开发资源链接地址
  5. Android之Sqlite模糊查询
  6. android app应用内更新
  7. ap与sta共存
  8. Android(安卓)Studio最终篇 - 架构
  9. Android(安卓)arm linux kernel启动流程(一)

随机推荐

  1. Android(安卓)ExpandableListActivity
  2. Android获取当前时间与星期几
  3. tabhost置底
  4. Android(安卓)周报
  5. Android(安卓)ConstraintLayout Toolbar
  6. Android练习之BitmapFactory.decodeFile
  7. android 游戏 让人物动起来
  8. Android加载大量文字时关键字变色
  9. 友盟统计配置
  10. Android(安卓)弹框没有背景色及背景边框