6

I want open what's app conversation activity cmp=com.whatsapp/.Conversation from my app.

How can I do this? I have contact phone number, contact id, contact raw id and also have what's app uri for a particular contact.

private void openWhatsApp(String id) {

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/"+id));
    Log.v("ssssss", s);
    i.setType("vnd.android.cursor.item/vnd.com.whatsapp.profile");
    i.setComponent(new ComponentName("com.whatsapp", ".Conversation"));
    startActivity(i);
}


04-20 18:13:45.794: I/ActivityManager(1862): START
{act=android.intent.action.VIEW
dat=content://com.android.contacts/data/8269
typ=vnd.android.cursor.item/vnd.com.whatsapp.profile
cmp=com.whatsapp/.accountsync.ProfileActivity} from pid 32159


04-20 18:42:11.317: I/ActivityManager(1862): START {flg=0x14000000 cmp=com.whatsapp/.Conversation (has extras)} from pid 1150
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • 1
    Adding the brief description and code has greatly improved this question from "I want code" to "I tried this, could you help?" (which makes a huge difference here.) – Sam Apr 20 '13 at 14:17
  • @Sam Thanks for your suggestion. I'll keep this in mind. – Sunny Apr 20 '13 at 14:23
  • It's not clear what you're asking about. Is the code not starting the correct app? (The logcat output suggests otherwise.) Is it crashing? Something else? – Ted Hopp Apr 21 '13 at 18:38

5 Answers5

6
private void openWhatsApp(String id) {

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
        new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
        new String[] { id }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));

startActivity(i);
c.close();
}

Where id is what's app uri like 0987654321@s.whatsapp.net

Matei Suica
  • 859
  • 1
  • 7
  • 17
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • 1
    ...how to get id .I think this is whats id .When i used your code. i have a getSherlockActivity() error .I am new in android so i dont understand what is getsherlocactivity and where it is used. I just want to send message from my application to whatsup without open whats up it is possible or not – Mahi Nov 16 '13 at 11:36
  • 1
    phoneNo@s.whatsapp.net is what's app id. Or you can get it from `data1` column in contacts.db. It is not possible to send message from your application to whatsup without open whats app. – Sunny Nov 18 '13 at 05:36
  • 2
    @MahiSingh i just remove getSherlockActivity() and its working perfectly. – BeingMIAkashs Dec 18 '13 at 07:12
  • @johnsmith How did you find this information ? Is this official? Does a similar code work for other social networks and apps, like FB-messenger, Viber, Line, Telegram ? – android developer Mar 13 '16 at 15:40
  • This code is giving me package not found exception!! Could not find any application to handle the intent. – Kshitij Aggarwal Jul 13 '16 at 13:44
2

try this code :

String smsNumber="919426640584@s.whatsapp.net";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Prakash Gajera");
i.setPackage("com.whatsapp");
startActivity(i);
Prakash Gajera
  • 563
  • 1
  • 6
  • 18
  • You need the contactId of the contact, cannot use the sms number. So it should look like something 189202@s.whatsapp.net where 189202 is the id of the contact. And that only launches the contact profile, WhatsApp still ignores sms_body and other EXTRA_TEXT values. – Genc Aug 22 '16 at 11:30
1

My final solution when the contact number is unknown to the user.

Optionally you can set a preformatted text also.

    try {
        String whatsAppRoot = "http://api.whatsapp.com/";
        String number = "send?phone=+xxxxxxxxxxx"; //here the mobile number with its international prefix
        String text = "&text=HERE YOUR TEXT";
        String uri = whatsAppRoot+number+text;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(uri));
        startActivity(intent);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), 
        "WhatsApp cannot be opened", Toast.LENGTH_SHORT).show();
    }
ivoroto
  • 925
  • 12
  • 12
0

You can use this example

startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse(
                                    "https://api.whatsapp.com/send?phone=+628119xxx&text=I'm%20interested%20in%20your%20car%20for%20sale"
                            )));
Latief Anwar
  • 1,833
  • 17
  • 27
0
 String KEY_QUICK_REPLY_TEXT = "Dear Valued Customer Thank you for contacting us your reference Number is "+refernceNumber ;
   Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+phone +"&text="+KEY_QUICK_REPLY_TEXT));
                            startActivity(intent);

Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
Steven
  • 2,258
  • 1
  • 22
  • 22