Android获取ArrayList中的所有联系人电话号码

问题描述:

我想将所有联系人的电话号码保存在ArrayList中,但我找不到方法。有没有办法让他们,而不是通过ContactsContract一个接一个地挑选他们?Android获取ArrayList中的所有联系人电话号码

+0

什么是联系人合同 – PSR 2013-03-06 09:22:14

+0

此问题已在[本] [1]主题中回答。请检查一下。 [1]:http://stackoverflow.com/a/12562234/1773155 – 2013-03-06 09:34:41

+0

肯定会套用你的需求使用[android-contact-extractor library](https://github.com/nitiwari-dev/android-接触器) – nitesh 2017-05-12 10:05:56

ContentResolver cr = mContext.getContentResolver(); //Activity/Application android.content.Context 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    if(cursor.moveToFirst()) 
    { 
     ArrayList<String> alContacts = new ArrayList<String>(); 
     do 
     { 
      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

      if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
      { 
       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null); 
       while (pCur.moveToNext()) 
       { 
        String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        alContacts.add(contactNumber); 
        break; 
       } 
       pCur.close(); 
      } 

     } while (cursor.moveToNext()) ; 
    } 
+0

谢谢!有效! – 2013-03-07 02:21:29

+0

什么是'contResv'? – tipycalFlow 2013-03-30 15:23:44

+2

contResv是ContentResolver的一个实例,你可以通过调用类android.content.Context的getContentResolver()方法来获取 – Santhosh 2013-04-02 04:43:18

试试这个:

Cursor managedCursor = getContentResolver() 
    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC"); 

并通过光标穿越,你可以存储在你选择的任何数据结构,所有这些数据。

+0

这是迄今为止最快的解决方案。 – milosmns 2016-10-19 16:49:56

+0

我不明白为什么其他答案与双循环无处不在,而这种解决方案是非常清洁! – 2017-12-11 21:17:26

试试这个也得到所有联系人。

Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null , null , null, 
         "upper("+Phone.DISPLAY_NAME + ") ASC"); 

此代码的工作速度比答案中的代码快得多,因为您不需要为每个联系人进行额外的查询。

private static final String CONTACT_ID = ContactsContract.Contacts._ID; 
private static final String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

private static final String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 
private static final String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 

public static ArrayList<String> getAll(Context context) { 
    ContentResolver cr = context.getContentResolver(); 

    Cursor pCur = cr.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      new String[]{PHONE_NUMBER, PHONE_CONTACT_ID}, 
      null, 
      null, 
      null 
    ); 
    if(pCur != null){ 
     if(pCur.getCount() > 0) { 
      HashMap<Integer, ArrayList<String>> phones = new HashMap<>(); 
      while (pCur.moveToNext()) { 
       Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID)); 
       ArrayList<String> curPhones = new ArrayList<>(); 
       if (phones.containsKey(contactId)) { 
        curPhones = phones.get(contactId); 
       } 
       curPhones.add(pCur.getString(pCur.getColumnIndex(PHONE_NUMBER))); 
       phones.put(contactId, curPhones); 
      } 
      Cursor cur = cr.query(
        ContactsContract.Contacts.CONTENT_URI, 
        new String[]{CONTACT_ID, HAS_PHONE_NUMBER}, 
        HAS_PHONE_NUMBER + " > 0", 
        null,null); 
      if (cur != null) { 
       if (cur.getCount() > 0) { 
        ArrayList<String> contacts = new ArrayList<>(); 
        while (cur.moveToNext()) { 
         int id = cur.getInt(cur.getColumnIndex(CONTACT_ID)); 
         if(phones.containsKey(id)) { 
          contacts.addAll(phones.get(id)); 
         } 
        } 
        return contacts; 
       } 
       cur.close(); 
      } 
     } 
     pCur.close(); 
    } 
    return null; 
} 
+1

不适合在这里,你永远不会使用PHONE_NUMBER。我认为你有一个PHONE_CONTACT_ID,你应该有PHONE_NUMBER – 2016-12-20 16:48:08

+0

@GregEnnis我现在无法检查,但我认为你是对的。我会编辑我的答案 – 2016-12-21 14:06:27

+0

除此之外,代码很好。谢谢 – 2016-12-22 14:45:46