Android Contacts的使用(二)

来源:互联网 发布:豆荚网络加速器注册 编辑:程序博客网 时间:2024/06/11 07:00

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();
复制代码

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机被伪基站覆盖怎么办 听了高频率声音怎么办 qq音乐签到没了怎么办 手机qq音乐不能播放怎么办 台式电脑放歌没有声音怎么办 微信图片上传大愎怎么办 行车记录仪内存卡丢了怎么办 投资项目失败lp的钱怎么办 无线网无ip分配怎么办 为什么电脑的暴风影音打不开怎么办 电枪充电板进水怎么办 捡到一颗子弹该怎么办 防弹衣只保护身体那手臂怎么办? 被子被宝宝尿湿怎么办 眼睛被子弹打了怎么办 gta5买了2套衣服怎么办 gta5车被摧毁了怎么办 gta5车被损坏了怎么办 头盔玻璃磨花了怎么办 浇花喷水壶坏了怎么办 电力专用光缆撞了怎么办 国防电缆挖断了怎么办 国防光缆挖断了怎么办 房门前乱挂光纤线影响住户怎么办 挂断低于限高的光缆怎么办 开大车挂住光缆怎么办 风把树枝挂断压到车该怎么办 货车柴油冻住了怎么办 尖头鞋老是折尖怎么办 打 氟氯西林疼怎么办 多余的十字绣线怎么办 硅胶类的东西沾到蓝药水怎么办? ph计斜率不到90怎么办 ph计斜率低于90怎么办 顾客说衣服起球怎么办 买的手机壳太滑怎么办 硅胶手机壳太滑怎么办 磨砂手机壳太滑怎么办 被热胶棒烫了怎么办 车钢垫子次了怎么办 【图】机组主轴密封漏水怎么办?