1

I want to send a text message via whatsapp from my app.

I do not know who the recepient is when i send the request to share via whatsapp i am directed to the contacts page.

How do I know which contact was chosen ?

This is the code I am using to share :

public void clickHandler(View view) {

PackageManager pm=getPackageManager();
try {

    Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.setType("text/plain");
    String text = "YOUR TEXT HERE";

    PackageInfo info=pm.getPackageInfo("com.whatsapp",PackageManager.GET_META_DATA);
    //Check if package exists or not. If not then code 
    //in catch block will be called
    waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivityForResult(Intent.createChooser(waIntent, "Share with"),1);

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  

}

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

    Log.d("whatsapp","requestCode:"+requestCode + " resultCode:"+resultCode+ " intent:"+data);
}

10-01 16:57:46.910: D/whatsapp(13522): requestCode:1 resultCode:0 intent:null
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • Why do you want to know which contact has been chosen? The app will automatically send the message to that contact which is what you want I guess. – Pedro Oliveira Oct 01 '14 at 14:07
  • I want to share a piece of info with another user via whatsapp, this user may not be my app user, and if this is the case, I want when this user does register for my app, to be able to give him the content that has been shared. and for that I need to flag this user – Lena Bru Oct 01 '14 at 14:11
  • If your onActivityResult doesn't trigger then there is no way of retrieving this because whatsApp is not coded for that. – Pedro Oliveira Oct 01 '14 at 14:13
  • Whatsapp doesn't mention anything about communicating back whom the message was sent to [in their docs](http://www.whatsapp.com/faq/en/android/28000012), so I imagine they don't. Also, think about the consequences if they did: where would you draw the line? Is just the contact's name still okay? How about their contact number? Email? Etc. Everyone will have a different opinion about this, hence I'd be surprised if Whatsapp would expose any data at all to 'outsiders' (read: other apps invoking the Whatsapp app) just like that. – MH. Oct 01 '14 at 14:16

1 Answers1

3

what you're trying to achieve is something the specific app you're sharing to (WhatsApp on your case) must have coded in their app, if they did not implement this functionality, you won't be able to achieve it. That is true for every single startActivityForResult on Android. If the app was not coded to return you the data, that means you can't have that data.

To check if any app implemented, the best thing to do is to investigate the contents of the objects returned by it.

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

put a break point in your code in the above line and on a debugging session check the contents of the data parameter. Check all the keys and values inside the Bundle returned by getExtras(), also check what's the Intent action.

As you pointed out on your question

D/whatsapp(13522): requestCode:1 resultCode:0 intent:null

that means no data is returned by what's app and what you want is not possible.

Budius
  • 39,391
  • 16
  • 102
  • 144