How can I open WhatsApp's conversation activity using contact data?

空扰寡人 提交于 2019-11-28 09:28:06
john smith
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

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);

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();
    }

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"
                            )));
Mudimba Moonde
 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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!