How to open WhatsApp using an Intent in your Android App

老子叫甜甜 提交于 2020-05-25 03:36:49

问题


I want an Intent to take control you directly to WhatsApp. So the moment the user clicks on the button, the Intent is supposed to take you to WhatsApp. This is the code I wrote after following a few guide lines but it doesn't work

buttonWhatsapp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Performs action on click
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(Intent.createChooser(sendIntent, ""));
            startActivity(sendIntent);
            //opens the portfolio details class
        }
    });

回答1:


Using the 2018 api:

String url = "https://api.whatsapp.com/send?phone="+number;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);



回答2:


This code working for me

String contact = "+00 9876543210"; // use country code with your phone number
    String url = "https://api.whatsapp.com/send?phone=" + contact;
    try {
         PackageManager pm = context.getPackageManager();
         pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
         Intent i = new Intent(Intent.ACTION_VIEW);
         i.setData(Uri.parse(url));
         startActivity(i);                            
    } catch (PackageManager.NameNotFoundException e) {
    Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
    e.printStackTrace();
    }



回答3:


This works perfectly 2018

numero = number phone

mensaje= message to send

private void openWhatsApp(String numero,String mensaje){

    try{
        PackageManager packageManager = getActivity().getPackageManager();
        Intent i = new Intent(Intent.ACTION_VIEW);
        String url = "https://api.whatsapp.com/send?phone="+ numero +"&text=" + URLEncoder.encode(mensaje, "UTF-8");
        i.setPackage("com.whatsapp");
        i.setData(Uri.parse(url));
        if (i.resolveActivity(packageManager) != null) {
            startActivity(i);
        }else {
            KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT);
        }
    } catch(Exception e) {
        Log.e("ERROR WHATSAPP",e.toString());
        KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT);
    }

}



回答4:


I am showing you how to share text and image both here, For sharing text you can use these code ,

private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
    share.putExtra(Intent.EXTRA_TEXT, "http://www.codeofaninja.com");

    startActivity(Intent.createChooser(share, "Share link!"));
}

Now if you want to share image then you can use these code ,

private void shareImage() {
    Intent share = new Intent(Intent.ACTION_SEND);

    // If you want to share a png image only, you can do:
    // setType("image/png"); OR for jpeg: setType("image/jpeg");
    share.setType("image/*");

    // Make sure you put example png image named myImage.png in your
    // directory
    String imagePath = Environment.getExternalStorageDirectory()
            + "/myImage.png";

    File imageFileToShare = new File(imagePath);

    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(share, "Share Image!"));
}



回答5:


The easiest way i know is by calling the following method (Use the String variable (message) to input the text you want to send via WhatAapp):

private void sendWhatsapp(String message){
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setType("text/plain");
    sendIntent.setPackage("com.whatsapp");
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(sendIntent);
    }
}

I hope this helps you.




回答6:


btnWhatsapp.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                startSupportChat ();
            }
        } );

private void startSupportChat() {

        try {
            String headerReceiver = "";// Replace with your message.
            String bodyMessageFormal = "";// Replace with your message.
            String whatsappContain = headerReceiver + bodyMessageFormal;
            String trimToNumner = "+910000000000"; //10 digit number
            Intent intent = new Intent ( Intent.ACTION_VIEW );
            intent.setData ( Uri.parse ( "https://wa.me/" + trimToNumner + "/?text=" + "" ) );
            startActivity ( intent );
        } catch (Exception e) {
            e.printStackTrace ();
        }

    }



回答7:


 PackageManager pm = getActivity().getPackageManager();

    try
    {
        // Raise exception if whatsapp doesn't exist
        PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        waIntent.setPackage("com.whatsapp");
        waIntent.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
        startActivity(waIntent);
    }
    catch (PackageManager.NameNotFoundException e)
    {
        Toast.makeText(MainActivity.activity, "Please install whatsapp app", Toast.LENGTH_SHORT)
                .show();
    }



回答8:


Hey this snippet is from the official whatsapp site

https://www.whatsapp.com/faq/android/28000012

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);



回答9:


Checkout this method

 private void openWhatsApp(String smsNumber) {
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hi, This is " + PreferenceManager.get(this, Constants.USERNAME));
    sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
    sendIntent.setPackage("com.whatsapp");
    if (sendIntent.resolveActivity(getPackageManager()) == null) {
        Toast.makeText(this, "Error/n", Toast.LENGTH_SHORT).show();
        return;
    }
    startActivity(sendIntent);
}



回答10:


this work for this days ago

 private void openWhatsApp(String number) {
    try {
        number = number.replace(" ", "").replace("+", "");

        Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
        sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number)+"@s.whatsapp.net");
       // getApplication().startActivity(sendIntent);

        startActivity(Intent.createChooser(sendIntent, "Compartir en")
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

    } catch(Exception e) {
        Log.e("WS", "ERROR_OPEN_MESSANGER"+e.toString());
    }
}


来源:https://stackoverflow.com/questions/38422300/how-to-open-whatsapp-using-an-intent-in-your-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!