以编程方式将whatsapp消息发送到特定号码

时间:2019-04-16 02:50:54

标签: java android

我正在开发一个应用,我想向特定号码发送WhatsApp消息

我使用了此代码并进行了更正。
但是同时安装了WhatsApp Messenger和商业WhatsApp的用户,此代码直接将消息发送到商业WhatsApp中,没有显示允许用户在两​​个应用之间进行选择的选项

在android 5中,它显示选项,但是在android 7和8中,它不显示选项。它仅发送给企业WhatsApp。

您能帮我允许在Android 7和8中的两个应用程序之间进行选择吗?

private void openWhatsApp (String phoneNumber) {

    boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");

    if (isWhatsappInstalled) {

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+phoneNumber));

        startActivity(intent);

    } else {
        Uri uri = Uri.parse("market://details?id=com.whatsapp");
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        Toast.makeText(getActivity(), "WhatsApp not Installed",
                Toast.LENGTH_SHORT).show();
        startActivity(goToMarket);
    }
}

private boolean whatsappInstalledOrNot(String uri) {
    PackageManager pm = getActivity().getPackageManager();
    boolean app_installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}

1 个答案:

答案 0 :(得分:1)

我正在借助以下代码从我的应用程序发送whatapp消息:-

private void sendMsgOnSavedWhatsappNumber(String mobile) {
        Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
        sendIntent.putExtra("jid",
                PhoneNumberUtils.stripSeparators("91" + mobile) + "@s.whatsapp.net");
        startActivity(sendIntent);
    }