簡體   English   中英

從Android聯系人數據庫獲取聯系人ID無法正常工作

[英]Getting Contact ID from Android Contacts database is not working as intended

我有以下代碼。

        int phoneContactID = new Random().nextInt();

    Cursor contactLookupCursor =  context.getContentResolver().query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(contactNumber)), new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},null,null,null);     

    try 
    {
        contactLookupCursor.moveToFirst();
        while(contactLookupCursor.moveToNext())
        {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
         }
    } 
    finally 
    {
        contactLookupCursor.close();
    }       

上面代碼中的問題是即使我在模擬器聯系人中給出現有數字,它也不會返回任何結果。 我正在測試它一小時后它工作正常,現在當我再次測試它,它沒有返回任何東西。 我不確定代碼是否有任何問題。 我想要做的是獲得一個與多個數字相匹配的ID。 例如,有一個名為“A”的聯系人姓名,A有兩個號碼。 基本上A的聯系人ID應該是1,無論我指的是哪個號碼。 我的假設是否正確?

更新:我做了一些測試。 假設一個號碼在沒有國家代碼的情況下存儲在聯系人數據庫中,如222-222-2222。 僅當我通過2222222222或222-222-2222時,使用以下代碼的搜索才會返回聯系人ID。 如果存儲相同的號碼,如12222222222,則僅當我搜索號碼為12222222222時才會收到有效的聯系人ID。

        String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
    Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(contactNumber));
    Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
    if (c.moveToFirst()) {
        phoneContactID = c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
    }

我不確定我在這里做錯了什么。 任何幫助,將不勝感激。

public static int getContactIDFromNumber(String contactNumber,Context context)
{
    contactNumber = Uri.encode(contactNumber);
    int phoneContactID = new Random().nextInt();
    Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
        while(contactLookupCursor.moveToNext()){
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
            }
        contactLookupCursor.close();

    return phoneContactID;
}

現在正在運行的代碼。

好的..抱歉我想念解釋你的問題我還沒有完成它。但我認為以下代碼可能有用了試試看,lemme知道:

Cursor phone_cursor = cr.query( 
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null,
                        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", 
                        new String[]{"YOUR NUMBER GOES HERE"}, null); 
                while (phone_cursor.moveToNext()) 
                { 

//here you can extract the inofrmation you want


phone_cursor.getString(             phone_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                }

按照以下方式執行以下步驟:

//此方法將返回一個包含整個信息的游標

private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,      
        };

        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

接下來,您可以遍歷此Cursor以獲取名稱和ID,如下所示:

while (cursor.moveToNext()) 
            {
                namess[i]= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                id[i] = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                i++;
            }

vikramjb答案很棒,我嘗試以更清晰的方式(通過使用https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html中的說明):

private Long GetContactIdFromNumber(ContentResolver contentResolver, String number)
{
    Uri contactLookup = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor contactLookupCursor = contentResolver.query(contactLookup,new String[] {ContactsContract.PhoneLookup._ID}, null, null, null);
    if (contactLookupCursor.moveToNext()){
        long ret = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        contactLookupCursor.close();
        return ret;
    }
    contactLookupCursor.close();
    return 0l;
}

順便說一句,在API的版本(21段)存在PHONE_ACCOUNT_IDCallLog.Callshttps://developer.android.com/reference/android/provider/CallLog.Calls.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM