其他分享
首页 > 其他分享> > Android查询phonenumber以获取rawcontactID

Android查询phonenumber以获取rawcontactID

作者:互联网

我想查询phonenumber以获取rawcontactID.

我唯一知道的联系是给定的phonenumber,但对于我的功能,我需要有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(); 
}

提前致谢

编辑:

我上面的代码工作正常,但我仍在寻找大量联系人的增加速度.因此,如果有人提供解决方案来结合我的查询,我会给予赏金.

解决方法:

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以提高效率.如果多个联系人具有相同的号码,也会将返回类型更改为数组.

标签:android,phone-number,contacts
来源: https://codeday.me/bug/20190704/1375510.html