简体   繁体   English

如何从电话联系人列表中获取特定联系人的详细信息?

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

what i want to do is i will provide a number and i want to get name by which that number is stored in the mobile contact list. 我想做的是,我将提供一个电话号码,我想获取将该电话号码存储在移动联系人列表中的名称。 I've tried many codes from different sites but none is fulfilling my requirement! 我尝试了来自不同站点的许多代码,但没有一个可以满足我的要求! I'm working on this since many hours any help will be appreciated. 由于许多小时的任何帮助,我们将不胜感激。 Thanks in advance! 提前致谢!

Currently i'm using this code: 目前,我正在使用此代码:

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

You can use this code to get all the contacts and then loop and check for the name according to the given phone number 您可以使用此代码获取所有联系人,然后根据给定的电话号码循环搜索并检查姓名

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

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM