Android Contacts的使用(二)

[size=x-small][b]API For 1.6 and Before 1.6之前的版本[/b][/size]

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

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


Retrieving Contact Details 获取联系方式[/b]

本的联系人信息存储在联系人表中,而详细信息存储在个人表中。在 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));
}
}
}
}

[b]
Phone Numbers 电话号码[/b]
查询的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++;
}
}

以存储多个电话号码,我们需要遍历返回的结果。查询除了返回电话号码外还返回了其他(家庭,工作,手机等)类型。
[b]
Email Addresses 邮件地址[/b]
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();

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

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

[b]Postal Addresses 邮政地址[/b]
每个联系人都可以存储多个邮政地址。地址存储在联系方式表中,要获取数据,需要有次要条件。添加适配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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值