简体   繁体   中英

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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