安卓得到ArrayList中的所有联系人的电话号码电话号码、联系人、ArrayList

2023-09-13 00:11:11 作者:゛拚命ㄣ微笑╭

我想保存的所有联系人的电话号码在一个ArrayList但我不能找到一种方法如何。有没有办法让他们,而不是挑选他们一个个ContactsContract?

解决方案

  ContentResolver的CR = mContext.getContentResolver(); //活动/应用android.content.Context
    光标光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
    如果(cursor.moveToFirst())
    {
        ArrayList的<字符串> alContacts =新的ArrayList<字符串>();
        做
        {
            字符串ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,新的String [] {ID},NULL);
                而(pCur.moveToNext())
                {
                    串contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    alContacts.add(contactNumber);
                    打破;
                }
                pCur.close();
            }

        }而(cursor.moveToNext());
    }
 

I am trying to save all contacts telephone numbers in an ArrayList but I cant find a way how. Is there a way to get them instead of picking them one by one with ContactsContract?

解决方案 iPhone如何设置联系人来电显示小头像

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()) ;
    }