web-dev-qa-db-fra.com

Comment ouvrir WhatsApp en utilisant une intention dans votre Android App

Je veux qu'une intention vous prenne le contrôle directement sur WhatsApp. Donc, au moment où l'utilisateur clique sur le bouton, l'intention est censée vous amener à WhatsApp. Ceci est le code que j'ai écrit après avoir suivi quelques lignes directrices, mais cela ne fonctionne pas

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
        }
    });
17
Prateek Mahesh

Utilisation de l'API 2018:

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

Ce code fonctionne pour moi

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();
    }
4
Ghanshyam Nayma
 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();
    }
1
Ali Gürelli

La façon la plus simple que je connaisse est d'appeler la méthode suivante (utilisez la variable String (message) pour saisir le texte que vous souhaitez envoyer 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);
    }
}

J'espère que ceci vous aide.

1
Octavio Orozco

Je vous montre comment partager du texte et des images ici, pour partager du texte, vous pouvez utiliser ce 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!"));
}

Maintenant, si vous souhaitez partager l'image, vous pouvez utiliser ce 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!"));
}
1
user6598062

Commander cette méthode

 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);
}
1
AMAN SINGH

Hé, cet extrait provient du site officiel de WhatsApp

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);
1
SaravInfern

ce travail pour il y a quelques jours

 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());
    }
}
0
joe06