Send Whatsapp message to specific contact

Sieryuu picture Sieryuu · Sep 19, 2013 · Viewed 41.5k times · Source

I followed this link and this is my code

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + "[email protected]"));
                i.setPackage("com.whatsapp");
                startActivity(i);

This is my log

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/data/[email protected] pkg=com.whatsapp }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
        at android.app.Activity.startActivityForResult(Activity.java:3351)
        at android.app.Activity.startActivityForResult(Activity.java:3312)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
        at android.app.Activity.startActivity(Activity.java:3522)
        at android.app.Activity.startActivity(Activity.java:3490)
        at com.sieryuu.maidchan.MainActivity.onClick(MainActivity.java:61)
        at android.view.View.performClick(View.java:4147)
        at android.view.View$PerformClick.run(View.java:17161)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)

My Question : How to send text to whatsapp contact in the background (without choose the contact number, I already know the ID)? Root if needed

Answer

Crono picture Crono · Aug 22, 2017

I found the right way to do this and is just simple after you read this article: http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/

phone and message are both String.

Source code:

try {
            PackageManager packageManager = context.getPackageManager();
            Intent i = new Intent(Intent.ACTION_VIEW);
            String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8");
            i.setPackage("com.whatsapp");
            i.setData(Uri.parse(url));
            if (i.resolveActivity(packageManager) != null) {
                context.startActivity(i);
            }
        } catch (Exception e){
            e.printStackTrace();
        }

Enjoy!