TA的每日心情 | 开心 2012-3-7 10:15 |
---|
签到天数: 11 天 [LV.3]偶尔看看II
|
HashMap<String, String> contacts = new HashMap<String, String>();
//联系人兼容问题处理
String contactUri="content://contacts/people";//联系人读取地址
String name="display_name";//联系人姓名列名
String number="number";//联系人电话;
if(Integer.valueOf(android.os.Build.VERSION.SDK)>=4){
number="data1";
contactUri="content://com.android.contacts/contacts";
}
Cursor cursor = null;
Cursor phone=null;
try {
// 1.5获取号码
cursor = context.getContentResolver().query(
Uri.parse(contactUri), null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
//联系人姓名
String peopleName=cursor.getString(cursor.getColumnIndex(name));
//联系人编号
String cid = cursor.getString(cursor.getColumnIndex("_id"));
//获取用户所有号码
if(Integer.valueOf(android.os.Build.VERSION.SDK)>=4){
phone= context.getContentResolver().query(
Uri.parse("content://com.android.contacts/data/phones"), new String[]{number},"contact_id="+cid, null, null);
}
else{
Uri personUri = ContentUris.withAppendedId(Contacts.People.CONTENT_URI,
Long.parseLong(cid));
//用户联系方式地址
Uri phonesUri = Uri.withAppendedPath(personUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
phone = context.getContentResolver().query(phonesUri,
new String[]{number}, null, null, null);
}
while(phone.moveToNext()){
//电话号码
String phoneNumber=phone.getString(0);
//要显示的信息
String key=peopleName+"("+phoneNumber+")";
if(!contacts.containsKey(key)){
contacts.put(key, phoneNumber);
}
}
phone.close();
cursor.moveToNext();
}
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
if(phone!=null){
phone.close();
phone=null;
}
}
return contacts; |
|