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

时间:2014-08-23 22:02:56

标签: android

我想要做的是我将提供一个号码,我想获得该号码存储在移动联系人列表中的名称。我尝试过来自不同网站的许多代码,但没有一个符合我的要求!我正在努力解决这个问题,因为很多时候任何帮助都会受到赞赏。提前谢谢!

目前我正在使用此代码:

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

1 个答案:

答案 0 :(得分:0)

您可以使用此代码获取所有联系人,然后循环并根据给定的电话号码检查姓名

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

希望它有所帮助!