android获取联系人总结

从android2.0开始,联系人的API做了很大的调整。
People接口由ContactsContract.Contacts代替。
 
要在AndroidMainfest.xml里面把权限打开:
<!-- 写权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<!-- 读权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
 
 
************************************************************************************
1.6
获取方法:
ContentResolver resolver = getContentResolver();
Cursor cursorPhone = resolver.query(People.CONTENT_URI,null,null , null,null);
 
2.1
获取方法:
ContentResolver resolver = getContentResolver();
Cursor cursorPhone = resolver.query(ContactsContract.Contacts.CONTENT_URI,null,null , null,null);
1.6将电话号码也放在同一个ContentProvider里,而2.1将号码放到了ContactsContract.CommonDataKinds.Phone.CONTENT_URI
 
 
仅获取手机号码:
ContentResolver resolver = getContentResolver();
Cursor cursorPhone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.TYPE+"="+ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE , null,null);
************************************************************************************
 
 
 
一、android联系人
2.xSDK版本
Uri: content://com.android.contacts/contacts
联系人姓名字段:display_name
 
 
1.6以下SDK
Uri:Contacts.People.CONTENT_URI;  (android.provider.Contacts.People.CONTENT_URI;)
联系人姓名字段:Contacts.People.NAME;
 
例如:
Uri personUri = null;
String columnName = null;
if(SDK_VERSION>4){//2.x,sdk版本

personUri = Uri.parse("content://com.android.contacts/contacts");

columnName = "display_name";
}else{//1.6以下SDK

personUri = Contacts.People.CONTENT_URI;

= Contacts.People.NAME;
}
 
 
二、短信箱
Cursor cursorInbox = getContentResolver().query(Uri.parse("content://sms/inbox"),

null, null, null, null);//获取收件箱信息,可以查询收件箱

Cursor cursorSendbox = getContentResolver().query(Uri.parse("content://sms/sent"),

null, null, null, null);//发件箱信息,可以查询发件箱
 
 
 
 
 
三、其中Uri表现形式
 
Android1.6及以前的版本中查询手机中的电话用android.provider.Contacts.People.CONTENT_URI;
1、android.provider.Contacts.People.CONTENT_URI;
 
在SDK2.X使用时,提示:
 
This class is deprecated.
 
The APIs have been superseded by ContactsContract.
 
The newer APIs allow access multiple accounts and support aggregation of similar contacts.
 
These APIs continue to work but will only return data for the first Google account created, which matches the original behavior.
 
 
SDK2.x使用如下Uri:
2、android.provider.ContactsContract.Contacts.CONTENT_URI;
3、android.provider.ContactsContract.Data.CONTENT_URI;


4、Uri uri = Uri.parse("content://contacts/people/");
//是一个可以从设备返回所有通讯录姓名列表的URI
 
// Get the base URI for People table in Contacts content provider.
 
 
   Uri uri = Uri.parse("content://contacts/people/23"); //是一个可以返回通讯录中ID=23的单行记录的URI字符串
 
6、Uri uri = Uri.parse("content://com.android.contacts/contacts");
 
 
获取电话号码用的Uri:
在Android2.0及以后(目前到了2.1,再往后不能保证无变化)用android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI进行查询。
android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
获取Email用的Uri:
ContactsContract.CommonDataKinds.Email.CONTENT_URI
 
四、读取SIM卡中的联系人
private Cursor getContacts() {

// Run query

Uri uri = Uri.parse("content://icc/adn");

//Contacts.People.CONTENT_URI;

//Uri uri = Uri.parse("content://sim/adn");

String[] projection = new String[] { "name",

"phone" };

String selection = null;

String[] selectionArgs = null;

String sortOrder = "name"//Contacts.People.NAME// Contacts.PeopleColumns.DISPLAY_NAME

+ " COLLATE LOCALIZED ASC";


return managedQuery(uri, projection, selection, selectionArgs,

sortOrder);
}
Android2.0中获取手机里联系人的方法
public void getContact(){

//获得所有的联系人   

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);   

//循环遍历   

if (cur.moveToFirst()) {

int idColumn  = cur.getColumnIndex(ContactsContract.Contacts._ID);   

int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

do {   

//获得联系人的ID号   

String contactId = cur.getString(idColumn);

//获得联系人姓名   

String disPlayName = cur.getString(displayNameColumn);   

//查看该联系人有多少个电话号码。如果没有这返回值为0   

int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));   

if(phoneCount>0){

   //获得联系人的电话号码   

   Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,   

ContactsContract.CommonDataKinds.Phone.CONTACT_ID   

+ " = " + contactId, null, null);   

   if(phones.moveToFirst()){

   do{

   //遍历所有的电话号码

   String phoneNumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  

   System.out.println(phoneNumber);   

   }while(phones.moveToNext());

   }

}

   } while (cur.moveToNext());

}   
}
 
在联系人的电话号码中有很多种,如果只想获得手机号码
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,   
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
            + " = " + contactId

+" and "+ ContactsContract.CommonDataKinds.Phone.TYPE+"="+ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, null, null);
-------------
转载
 
posted on 2012-10-02 15:18  Fra~~kaka  阅读(21790)  评论(0编辑  收藏  举报