erdomester
erdomester

Reputation: 11829

How to get contact id by phone number

I query the phone's calllog into a ListView. So when the user long clicks an item, a dialog comes up with options, including "View contact". To be able to view the contact the intent needs the contact id. My problem is that I not always get to see the right contact. I click on Peter, and Peter's contact sheet comes up. I click on Sarah, and Jason's contact sheet comes up.

I must have been using this code the wrong way. Please help.

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));

Cursor cursor =  contentResolver.query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);

if(cursor!=null) {
      while(cursor.moveToNext())
      {
        String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        contactid2 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
        }
        cursor.close();
}

Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid2));
 startActivity(intent_contacts);

Maybe what I need is not the PhoneLookup._ID, but some other ID.

I also tried Felipe's answer here, same happens.

Edit:

What the hell is going on???

Upvotes: 4

Views: 8758

Answers (2)

erdomester
erdomester

Reputation: 11829

After digging myself into theme, I found sven's help on googlegroups. The problem was that I was using a depracated intent. I read through many pages on the developer site, I must have missed it somehow.

I also post the full code.

contactid26 = null;

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));

Cursor cursor =  
contentResolver.query(
    uri, 
    new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
    null, 
    null, 
    null);

    if(cursor!=null) {
        while(cursor.moveToNext()){
            String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
            contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));

        }
        cursor.close();
    }
    if (contactid26 == null) {
        Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
    }
    else {
        Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
        //Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26));
        startActivity(intent_contacts);
    }

Upvotes: 4

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10633

Here in this case i am going to show you that how to get the ID, and Name of any contact no, which you have enter and want to search, For Example if you enter a no and want to search its Name and id then use this code, it is working 100%, if you want further modification in this, then you can tell me

         Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phnno.getText().toString()));//phone no 
         String[] proj = new String[] 
           {///as we need only this colum from a table...
             Contacts.DISPLAY_NAME,
             Contacts._ID,
            };
         String id=null;
         String sortOrder1 = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
         Cursor crsr = getContentResolver().query(lookupUri,proj, null, null, sortOrder1);
     while(crsr.moveToNext())
     {
         String name=crsr.getString(crsr.getColumnIndex(Contacts.DISPLAY_NAME));
          id = crsr.getString(crsr.getColumnIndex(Contacts._ID));
          Toast.makeText(this,"Name of this cntct is   : " +name +"\n id is : "+id ,Toast.LENGTH_SHORT).show();
     }
    crsr.close();

Upvotes: 1

Related Questions