阅读:5556回复:1

安卓系统联系人相关操作

楼主#
更多 发布于:2019-11-21 20:47
读取安卓系统通讯录列表

private static void getPhoneContacts(Context mContext) {
       mContactsList.clear();
       Cursor cursor = null;
       try {
           Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            // 这里是获取联系人表的电话里的信息  包括:名字,名字拼音,联系人id,电话号码;
           // 然后在根据"sort-key"排序
           cursor = mContext.getContentResolver().query(uri,
                   new String[] { Phone.DISPLAY_NAME, Phone.SORT_KEY_PRIMARY, Phone.CONTACT_ID,
                           Phone.DATA1 }, null, null, Phone.SORT_KEY_PRIMARY);
           if (cursor.moveToFirst()) {
               do {  
                   String phonenumber = cursor.getString(cursor
                                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   //Log.d(TAG, "......contact_phone....."+contact_phone);
                   String phonename = cursor.getString(0);
                   String sortKey = getSortKey(cursor.getString(1));
                   int contact_id = cursor.getInt(cursor
                                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                   if (!phonename.contains(mContext.getResources().getString(R.string.telephone_line))){
                       Contact contact = new Contact(phonenumber,phonename,sortKey,contact_id);
                       mContactsList.add(contact);
                   }  
               } while (cursor.moveToNext());
               cursor.close();
               //mHandler.sendEmptyMessage(ADAPTERREFRESH);
               if (recent_fragment == PublicData.ALLCONTACTSFRAGMENT) {
                   AllContactsFragment.getHandler().sendEmptyMessage(AllContactsFragment.ADAPTERREFRESH);
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }    }
沙发#
发布于:2019-11-22 08:54
仅供参考
游客

返回顶部