直接贴代码不解释
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 listshowif (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 listwhile (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 cancontactentity.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}}}

更多相关文章

  1. java和Android文件下载断点续传和图片下载代码实现,可直接复制简
  2. Android 使用 DowanloadManager 实现下载并获取下载进度实例代码
  3. Android Design: 九种常见Activity及代码解析之"Login Activity"
  4. [CSDN]Android应用程序进程启动过程的源代码分析
  5. Android开源项目源代码分享
  6. 如何用代码区分android TV和手机? 适配Android TV ?
  7. java Android 算一定业务规定的工作日(除去法定节假日、调休周末
  8. Anroid源代码编译相关资源

随机推荐

  1. android -> 修改自动转为大写 的textAllC
  2. Android中如何设置RadioButton在文字的右
  3. Android(安卓)start from now on
  4. android对话框大全
  5. Android: INSTALL_FAILED_UPDATE_INCOMPAT
  6. android 中RecyclerView 嵌套了 GridView
  7. Android源代码查看途径
  8. android官方技术文档翻译——Android(安
  9. Android梳理 Fragment
  10. Android(安卓)仿微信Activity左右切换 【