Android读取SIM联系人信息

Android1.6及以前的版本中查询手机中的电话用android.provider.Contacts.People.CONTENT_URI
在Android2.0及以后(目前到了2.1,再往后不能保证无变化)用android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI

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

}
}


在联系人的电话号码中有很多种,如果只想获得手机号码。代码如下:

Java代码
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);

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywxpf.html