packageyinger.firewall.util;importjava.util.ArrayList;importjava.util.HashMap;importandroid.content.ContentResolver;importandroid.content.Context;importandroid.database.Cursor;importandroid.provider.ContactsContract;importandroid.provider.ContactsContract.PhoneLookup;/*** ReadContactsUtil
*
*@authorYing_er
* @Email melody.crazycoding@gmail.com
* @time 2011/03/30 11:57:23
*@version1.00*/publicfinalclassMyReadContactsUtil {/***@returncontacts list*/publicstaticArrayList getContacts(Context context) {
ArrayList>list=newArrayList>();
String number="";
ContentResolver cr=context.getContentResolver();//得到一个ContentResolver对象//得到一个游标查询对象Cursor cursor=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);while(cursor.moveToNext()) {//获取联系人的名字的索引intnameFieldColumnIndex=cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME);//获取联系人的名字String contact=cursor.getString(nameFieldColumnIndex);
Cursor target=context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
target.moveToFirst();//得到联系人的IDString contentId=cursor.getString(target
.getColumnIndex(ContactsContract.Contacts._ID));
String isPnone=cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));if(Integer.parseInt(isPnone)>0) {
Cursor phone=context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contentId,null,null);while(phone.moveToNext()) {//得到联系人电话number=phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMapmap=newHashMap();
map.put("number", number);
map.put("name", contact);number="";
list.add(map);
}
phone.close();
}
}
cursor.close();returnlist;
}
}