簡體   English   中英

如何從電話聯系人列表中獲取特定聯系人的詳細信息?

[英]How to get the details of a particular contact from the phone contact list?

我想做的是,我將提供一個電話號碼,我想獲取將該電話號碼存儲在移動聯系人列表中的名稱。 我嘗試了來自不同站點的許多代碼,但沒有一個可以滿足我的要求! 由於許多小時的任何幫助,我們將不勝感激。 提前致謝!

目前,我正在使用此代碼:

Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL,  Uri.encode(incomingNumber));
        Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
                    String name = null;
    Cursor cursor = context.getContentResolver().query(uri,new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, null, null, null);
                        Toast.makeText(context, p.getString(cursor.getString(0), "unknown"), Toast.LENGTH_LONG).show();
                        // Invoke endCall()
                         if (cursor != null && cursor.moveToFirst()) {

                             editor1.putBoolean("fromcontacts", true);
                             editor1.putBoolean("notfromcontacts", false);
                             editor1.putString("incomingnumbername", cursor.getString(0));
                             editor1.commit();
                            // Toast.makeText(context, p.getString("incomingnumbername", "unknown"), Toast.LENGTH_LONG).show();
                         }

                         else
                         {
                             editor1.putBoolean("notfromcontacts", true);
                             editor1.putBoolean("fromcontacts", false);
                             editor1.putString("incomingnumbername", "Unknown");
                             editor1.commit();
                     Toast.makeText(context, p.getString("incomingnumbername", "unknown"), Toast.LENGTH_LONG).show();
                         }

您可以使用此代碼獲取所有聯系人,然后根據給定的電話號碼循環搜索並檢查姓名

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(
                        cur.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 phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(NativeContentProvider.this, "Name: " + name + ",  Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                     }
                    pCur.close();
                }
            }
       }

希望能幫助到你 !

暫無
暫無

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

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