1

I'm trying to select the Whatsapp address of a contact from my app, but given the fact that my app is a util for Whatsapp I want users to keep engaged with the native way of selecting a contact, that way they won't have to get used to a new way of doing things which is annoying for most of us.

I've done some research and I'm aware of links like Send text to specific contact (whatsapp), Sending message through WhatsApp, and some other questions and answers in the site, but they don't use the Whatsapp's contact picker.

Hope you can help.

Community
  • 1
  • 1
Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27

1 Answers1

6

After playing around for a long while I realized that we can launch the Whatsapp's contact picker activity and receive the results back, but not in the returned uri (the way Android contact picker does) but as an extra. I've come up with the following solution.

To start the Whatsapp's native contact picker we call the following code from our activity:

Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setPackage("com.whatsapp");
    try{
        startActivityForResult(intent, REQUEST_CODE_PICK_WHATSAPP);
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, R.string.notif_no_whatsapp, Toast.LENGTH_SHORT).show();  //no activity found to handle this intent means whatsapp is not installed
    }

Then, inside activity's onActivityResult(...) method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
    case REQUEST_CODE_PICK_WHATSAPP:
        if(resultCode == RESULT_OK){
            if(intent.hasExtra("contact")){
                String address = intent.getStringExtra("contact");
                Log.d(TAG, "The selected Whatsapp address is: "+address);
            }
        }
        break;

    default:
        break;
    }
}

Hope it contributes to make more apps that integrate with Whatsapp seamlessly.

Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27
  • Do you know how to share something and retrieve the adress? – Pedro Teran Apr 24 '14 at 19:29
  • I don't get the question. Could you be more specific in what you wanna do? – Junior Buckeridge Apr 30 '14 at 15:49
  • don't matter, I was trying to send a message through Action.sendto and retrieve the id of the selected user but is already solved. – Pedro Teran Apr 30 '14 at 16:30
  • Hi what is the value of REQUEST_CODE_PICK_WHATSAPP ? – SWAppDev Nov 24 '15 at 10:10
  • A custom value within your app to use in #onActivityResult(...). You can set it to 0 if you don't have any other request code set to that. – Junior Buckeridge Nov 24 '15 at 20:28
  • Hi! This intent implementation gives a contact list but, it has a new group creation option on the top of the list which leads to whatspp and out of our application. Is their any way to implement the filter in Contacts intent itself. Please check this once : http://stackoverflow.com/questions/38741298/how-to-filter-a-contact-picker-intent-on-the-basis-of-account-type – Shail Adi Aug 03 '16 at 12:17