0

I'm developing an app, and I want to send a WhatsApp message to a specific number

I used this code from Satheesh's answer here and make corrections.
but the users who installed both WhatsApp messenger and business WhatsApp, this code send a message into business WhatsApp directly, it didn't show options to allow the users to choose between two apps

in android 5 it shows options but android 7 and 8 it didn't show options. it sends to business WhatsApp only.

can you help me to allow to give options to choose between both apps in android 7 and 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;
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Noor Kh.
  • 51
  • 4

1 Answers1

1

i am sending whatapp message from my app with the help of this code:-

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);
    }
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17