直接贴代码不解释
private void getCursors() {
Cursor phoneCursor = this.managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
Cursor callLogCursor = this.managedQuery(CallLog.Calls.CONTENT_URI ,
new String[] { CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE },
"1=1) group by "+CallLog.Calls.NUMBER+" -- (", null, null);
// update listshow
if (0 < phoneCursor.getCount()||0 < callLogCursor.getCount()) {
updateCursorList(phoneCursor,callLogCursor);
}
}
private void updateCursorList(Cursor phoneCursor,Cursor callLogCursor) {
Cursor contactCursor = null;
customArrayList.clear();
Log.i(TAG, "callLogCursor.getCount()" + callLogCursor.getCount());
if(0 < callLogCursor.getCount()){
callLogCursor.moveToFirst();
while (callLogCursor.getPosition() != callLogCursor.getCount()) {
String number = callLogCursor.getString(callLogCursor
.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
String name = callLogCursor.getString(callLogCursor
.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
if(null == name){
ContactEntity contactentity = new ContactEntity();
contactentity.contacts_display_name = number;
contactentity.contacts_phone_number = number;
customArrayList.add(contactentity);
}
callLogCursor.moveToNext();
}
}
Log.i(TAG, "phoneCursor.getCount()" + phoneCursor.getCount());
if(0 < phoneCursor.getCount()){
phoneCursor.moveToFirst();
// find all contact list
while (phoneCursor.getPosition() != phoneCursor.getCount()) {
ContactEntity contactentity = new ContactEntity();
contactentity.contact_id = phoneCursor
.getLong(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
contactentity.contacts_phone_type = phoneCursor
.getInt(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
contactentity.contacts_phone_number = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(
"-", "");
contactCursor = this.managedQuery(
ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts._ID + "="
+ contactentity.contact_id, null, null);
contactCursor.moveToFirst();
contactentity.contacts_display_name = contactCursor
.getString(contactCursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace(
"-", "");
// contactentity.contact_photo_id = contactCursor.getInt(contactCursor
// .getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
// if (0 != contactentity.contact_photo_id) {
// Uri peopleURI = ContentUris.withAppendedId(People.CONTENT_URI,
// new Long(contactentity.contact_photo_id));
// contactentity.contact_phone_bmp = People.loadContactPhoto(this,
// peopleURI, 0, null);
// }
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
contactentity.contact_id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);
if(null != input){
contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input);
}
// spell name can
contactentity.spellName = PinYin.getInstance(this).getPinyinString(
contactentity.contacts_display_name);
Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id
+ " contactentity.contacts_phone_type: "
+ contactentity.contacts_phone_type
+ " contactentity.contacts_phone_number: "
+ contactentity.contacts_phone_number
+ "contactentity.contacts_display_name: "
+ contactentity.contacts_display_name
+ "contactentity.contact_phone_bmp: "
+ contactentity.contact_phone_bmp);
phoneCursor.moveToNext();
customArrayList.add(contactentity);
}
}
Collections.sort(customArrayList, new Comparator<ContactEntity>() {
public int compare(ContactEntity o1, ContactEntity o2) {
ContactEntity he1 = o1, he2 = o2;
return he1.getName(ContactEntity.SPELL_NAME)
.compareToIgnoreCase(
he2.getName(ContactEntity.SPELL_NAME));
}
});
if (null != phoneCursor|| null!= callLogCursor) {
if (null != mAdapter) {
mAdapter.list = customArrayList;
mAdapter.notifyDataSetChanged();
} else {
mAdapter = new ContactAdapter(customArrayList);
mContactList.setAdapter(mAdapter);
}
try {
if(0 < phoneCursor.getCount()){
phoneCursor.close();
contactCursor.close();
}
if(0 < callLogCursor.getCount()){
callLogCursor.close();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
// ignore
}
}
}