4

我想查询电话号码以获取 rawcontactID。

我唯一知道的联系人是给定的电话号码,但对于我的功能,我需要有 rawcontactID。我得到了一个工作代码,但现在我确实使用了 2 个单独的查询。我想要的是 1 个查询,可以同时执行这两项操作以节省一些查询时间。

我的代码:

    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber));       
    String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };     
    Cursor cursor = contentResolver.query(uri, columns, null, null, null); 

if(cursor!=null) { 
    int clenght = cursor.getCount();
    while(cursor.moveToNext()){ 
     //contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)); 
     id = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID)); 

    } 
    cursor.close(); 
}

Cursor pCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data.RAW_CONTACT_ID}, ContactsContract.Data.CONTACT_ID+" = "+ id, null, null); 
if(pCur!=null) { 
    int clenght = pCur.getCount();
    while(pCur.moveToNext()){ 
     //contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)); 
     id = pCur.getString(pCur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); 

    } 
    pCur.close(); 
}

提前致谢

编辑:

我上面的代码工作正常,但我仍在为大量联系人寻找提高速度。因此,如果有人提供解决方案来组合我的查询,我将给予奖励。

4

2 回答 2

2
private String[] getRawContactIdFromNumber(String givenNumber){
        List<String> rawIds = new ArrayList<String>();
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID},ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+ givenNumber +"'",null, ContactsContract.CommonDataKinds.Phone.NUMBER);

        while (phones.moveToNext())
        {
          rawIds.add( phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID)));
          Log.v("contacts","Given Number: " + givenNumber + "Raw ID: " +rawIds.get(rawIds.size() - 1));
        }
        phones.close();
        String[] ret = new String[0];

        return rawIds.toArray(ret);
    }

编辑为仅在光标中包含原始 id 以提高效率。如果多个联系人具有相同的号码,还将返回类型更改为数组。

于 2012-11-24T04:13:30.577 回答
0

请试试

String phonenumber = "input your phone number";
        Cursor pCur = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[] { ContactsContract.Data.RAW_CONTACT_ID,
                        Phone.CONTACT_ID }, Phone.NUMBER + " = " + phonenumber,
                null, null);


if (pCur != null) {
                while (pCur.moveToNext()) {
                    String contactID = pCur.getString(pCur
                            .getColumnIndex(Phone.CONTACT_ID));
                    String Rowid = pCur.getString(pCur
                            .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                    Log.e("RAW_CONTACT_ID", Rowid);
                    Log.e("CONTACT_ID", contactID);
                }
                pCur.close();
            }   

现在您可以在单个查询中获得 Both CONTACT_ID& 。RAW_CONTACT_ID

于 2012-11-23T11:37:31.230 回答