Android 获取Contacts 联系人 姓名 号码 照片信息

直接贴代码不解释

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
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值