How to share text to WhatsApp from my app?

前端 未结 9 1223
醉话见心
醉话见心 2020-12-04 12:27

I develop an app with a functionality for sharing text. This is working fine except for WhatsApp. What should I do? Is there any specific API for that?

9条回答
  •  青春惊慌失措
    2020-12-04 12:54

    If the user does not have the Whatsapp app in their device then the user will get the ActivityNotFoundException

    Then, you should move the user to download the app first.

    public void shareViaWhatsApp() {
            Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
            whatsappIntent.setType("text/plain");
            whatsappIntent.setPackage("com.whatsapp");
            whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Application of social rating share with your friend");
            try {
                Objects.requireNonNull(getActivity()).startActivity(whatsappIntent);
            } catch (android.content.ActivityNotFoundException ex) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")));
            }
        }
    

提交回复
热议问题