在 android 工作室向 WhatsApp 中的特定联系人发送消息

Send message to specific contact in WhatsApp on android studio

我想知道如何在我自己的应用程序中通过联系人姓名向 WhatsApp 发送消息。

找了几个类似的问题,但是只能用phone号发消息,比如这个

Send text to specific contact programmatically (whatsapp)

谁能帮我解决这个问题? 非常感谢!

您只需执行以下代码:

try {
        Intent sendMsg = new Intent(Intent.ACTION_VIEW);
        String url = "https://api.whatsapp.com/send?phone=" + "+92 1111111111" + "&text=" + URLEncoder.encode("Your Message to Contact Number", "UTF-8");
        sendMsg.setPackage("com.whatsapp");
        sendMsg.setData(Uri.parse(url));
        if (sendMsg.resolveActivity(getPackageManager()) != null) {
            startActivity(sendMsg);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Phone 号码应该是正确的格式,因为我已经传递了 92 作为国家代码和其他 10 位数字作为您的 Phone 号码,您也可以传递您想要的消息。