Simone Avogadro
Simone Avogadro

Reputation: 819

Android intent to call a contact, not a number

Context:

Question:

I want to initiate a call to a Contact not to a number so that Android:

I could of course implement a popup from scratch but I'd prefer to delegate to a standard action so that the user has the very same UX as using the standard dialer.

Upvotes: 1

Views: 2048

Answers (1)

Di Vero Labs
Di Vero Labs

Reputation: 344

You can use something like this to prompt the user to choose the contact, then which phone number to call (if there is more than one)... than pass that to the intent to make the call:

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {  
    Uri contactData = data.getData();                   
    String theID = contactData.toString());

    //MAKE YOUR CALL .. do whatever... example:
    ContentResolver contentResolver = getContentResolver();
    Uri contactData = Uri.parse(theID);
    Cursor cur = contentResolver.query(contactData, null, null, null, null);
    String theNumber = cur.getString(cur.getColumnIndex("data4"));
    cur.close();

    Intent my_callIntent = new Intent(Intent.ACTION_CALL);
    my_callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    my_callIntent.setData(Uri.parse("tel:" + theNumber));
    startActivity(my_callIntent);


    }                   

}  

Its not pretty or perfect, probably needs some modifications, just kinda going off the top of my head but hopefully you get the idea.

Upvotes: 2

Related Questions