Wysyłanie wiadomości przez WhatsApp

Ponieważ znalazłem kilka starszych postów, które mówią, że whatsapp nie obsługuje tego, zastanawiałem się, czy coś się zmieniło i czy istnieje sposób, aby otworzyć whatsapp "czat" z numerem, który wysyłam za pośrednictwem intencji?

Author: Manan Sharma, 2013-03-17

15 answers

UPDATE Proszę odnieść się do https://faq.whatsapp.com/en/android/26000030/?category=5245251

Funkcja WhatsApp Click To Chat pozwala rozpocząć czat z kogoś bez zapisania numeru telefonu w telefonie Książka Adresowa. O ile znasz numer telefonu tej osoby, możesz Utwórz link, który pozwoli Ci rozpocząć z nimi czat.

Użycie: https://api.whatsapp.com/send?phone=15551234567

Don ' t użycie: https://api.whatsapp.com/send?phone=+001-(555)1234567

Przykład: https://api.whatsapp.com/send?phone=15551234567&text=I'm%20interested%20in%20your%20car%20for%20sale

Oryginalna odpowiedź Oto rozwiązanie

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  

}

Zobacz też http://www.whatsapp.com/faq/en/android/28000012

 154
Author: Manan Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-08 09:57:18

Za pomocą tego kodu możesz otworzyć czat whatsapp z podanym numerem.

void openWhatsappContact(String number) {
    Uri uri = Uri.parse("smsto:" + number);
    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
    i.setPackage("com.whatsapp");  
    startActivity(Intent.createChooser(i, ""));
}
 54
Author: user2957782,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-18 15:16:23

Znalazłem następujące rozwiązanie, najpierw potrzebujesz id whatsapp:

Dopasowanie do Raportów z innych wątków tutaj i na innych forach nazwa loginu, którą znalazłem, była rodzajem: Międzynarodowy Numer Kierunkowy Bez 0 lub + na początku + numer telefonu bez pierwszego 0 + @s.whatsapp.net

Na przykład, jeśli mieszkasz w Holandii i masz numer telefonu 0612325032 to [email protected] - > + 31 dla Holandii bez 0 LUB + i numer telefonu bez 0.

public void sendWhatsAppMessageTo(String whatsappid) {

Cursor c = getSherlockActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
        new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
        new String[] { whatsappid }, null);
c.moveToFirst();

Intent whatsapp = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
c.close();

 if (whatsapp != null) {

startActivity(whatsapp);      

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

}
 24
Author: Diego,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-30 00:24:39

To powinno działać niezależnie od tego, czy Whatsapp jest zainstalowany, czy nie.

boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
        if (isWhatsappInstalled) {
            Uri uri = Uri.parse("smsto:" + "98*********7")
            Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Hai Good Morning");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(sendIntent);
        } else {
            Toast.makeText(this, "WhatsApp not Installed",
                    Toast.LENGTH_SHORT).show();
            Uri uri = Uri.parse("market://details?id=com.whatsapp");
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(goToMarket);

        }

private boolean whatsappInstalledOrNot(String uri) {
    PackageManager pm = 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;
}
 12
Author: Satheesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-30 13:32:09

Testowany na Marshmallow S5 i działa!

    Uri uri = Uri.parse("smsto:" + "phone number with country code");
    Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent); 

Otworzy to bezpośredni Czat z osobą, jeśli whatsup nie jest zainstalowany, to rzuci wyjątek , Jeśli numer telefonu nie jest znany whatsup, zaoferuje wysłanie zaproszenia przez sms lub zwykłą wiadomość sms

 9
Author: Ilya Gazman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:44

Spróbuj tego, ten kod uruchom WhatsApp przez Intent.ACTION_VIEW, nie zapomnij użyć kodu kraju do numeru telefonu.

startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(
                                "https://api.whatsapp.com/send?phone=+628119xxx&text=I'm%20interested%20in%20your%20car%20for%20sale"
                        )));
 4
Author: Latief Anwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-26 21:12:04

To mi działa:

PackageManager pm = context.getPackageManager();
try {
    pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
    Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName,
                    ri.activityInfo.name));
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, element);

} catch (NameNotFoundException e) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
 2
Author: Cabezas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-03 10:47:18

Poniższy kod jest używany przez aplikację Google Now i nie będzie działał dla żadnej innej aplikacji.

Piszę ten post, ponieważ denerwuje mnie, że WhatsApp nie pozwala innym programistom wysyłać wiadomości bezpośrednio z wyjątkiem Google.

I chcę, aby inni niezależni programiści wiedzieli, że tego rodzaju współpraca trwa, podczas gdy Google wciąż mówi o "open for anyone", a WhatsApp mówi, że nie chcą zapewnić żadnego dostępu do deweloperów.

Ostatnio WhatsApp dodał intencję specjalnie dla Google Now, która powinna wyglądać następująco:

Intent intent = new Intent("com.google.android.voicesearch.SEND_MESSAGE_TO_CONTACTS");
intent.setPackage("com.whatsapp");
intent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.VoiceMessagingActivity"));

intent.putExtra("com.google.android.voicesearch.extra.RECIPIENT_CONTACT_CHAT_ID", number);
intent.putExtra("android.intent.extra.TEXT", text);
intent.putExtra("search_action_token", ?????);

Mógłbym się również dowiedzieć, że "search_action_token" jest PendingIntent zawiera obiekt IBinder, który jest wysyłany z powrotem do aplikacji Google i sprawdzany, czy został utworzony przez Google Now.

W Przeciwnym Razie WhatsApp nie zaakceptuje wiadomości.

 2
Author: black-hawk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-16 22:16:19

Obecnie, jedynym oficjalnym API , które można zrobić GET wniosek do:

https://api.whatsapp.com/send?phone=919773207706&text=Hello

W każdym razie, istniejetajny program API już uruchomiony przez WhatsApp

 2
Author: student,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-16 10:03:12

To jest dużo długie, ale gburowate pracy. miłego kodowania:)

 //method used to show IMs
private void show_custom_chooser(String value) {
    List<ResolveInfo> list = null;
    final Intent email = new Intent(Intent.ACTION_SEND);
    email.setData(Uri.parse("sms:"));
    email.putExtra(Intent.EXTRA_TEXT, "" + value);
    email.setType("text/plain"); // vnd.android-dir/mms-sms

    WindowManager.LayoutParams WMLP = dialogCustomChooser.getWindow()
            .getAttributes();
    WMLP.gravity = Gravity.CENTER;
    dialogCustomChooser.getWindow().setAttributes(WMLP);
    dialogCustomChooser.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialogCustomChooser.setCanceledOnTouchOutside(true);
    dialogCustomChooser.setContentView(R.layout.about_dialog);
    dialogCustomChooser.setCancelable(true);
    ListView lvOfIms = (ListView) dialogCustomChooser
            .findViewById(R.id.listView1);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> launchables = pm.queryIntentActivities(email, 0);
    // ////////////new
    list = new ArrayList<ResolveInfo>();
    for (int i = 0; i < launchables.size(); i++) {
        String string = launchables.get(i).toString();
        Log.d("heh", string);
//check only messangers
        if (string.contains("whatsapp")) {
            list.add(launchables.get(i));
        }
    }
    Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm));
    int size = launchables.size();
    adapter = new AppAdapter(pm, list, MainActivity.this);
    lvOfIms.setAdapter(adapter);
    lvOfIms.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            ResolveInfo launchable = adapter.getItem(position);
            ActivityInfo activity = launchable.activityInfo;
            ComponentName name = new ComponentName(
                    activity.applicationInfo.packageName, activity.name);
            email.addCategory(Intent.CATEGORY_LAUNCHER);
            email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            email.setComponent(name);
            startActivity(email);
            dialogCustomChooser.dismiss();
        }
    });
    dialogCustomChooser.show();

}
 1
Author: John smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-22 08:05:18

Jestem naprawdę spóźniony, ale wierzę, że obecnie mamy krótsze i lepsze rozwiązania do wysyłania wiadomości za pośrednictwem WhatsApp.

Możesz użyć poniższego przycisku, aby wywołać selektor systemu, a następnie wybrać aplikację, której użyjesz, aby udostępnić cokolwiek chcesz.

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

Jeśli naprawdę musisz wysłać przez WhatsApp, wystarczy, że wykonasz następujące czynności (pominiesz selektor systemu)

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

Jeśli potrzebujesz więcej informacji, możesz je znaleźć tutaj: WhatsApp FAQ

 1
Author: Will,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-07 08:45:39

Sprawdź ten kod,

    public void share(String subject,String text) {
     final Intent intent = new Intent(Intent.ACTION_SEND);

String score=1000;
     intent.setType("text/plain");
     intent.putExtra(Intent.EXTRA_SUBJECT, score);
     intent.putExtra(Intent.EXTRA_TEXT, text);

     startActivity(Intent.createChooser(intent, getString(R.string.share)));
}
 0
Author: Basim Sherif,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-28 08:43:40

To mi działa:

public static void shareWhatsApp(Activity appActivity, String texto) {

    Intent sendIntent = new Intent(Intent.ACTION_SEND);     
    sendIntent.setType("text/plain");
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, texto);

    PackageManager pm = appActivity.getApplicationContext().getPackageManager();
    final List<ResolveInfo> matches = pm.queryIntentActivities(sendIntent, 0);
    boolean temWhatsApp = false;
    for (final ResolveInfo info : matches) {
      if (info.activityInfo.packageName.startsWith("com.whatsapp"))  {
          final ComponentName name = new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
          sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          sendIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);
          sendIntent.setComponent(name);
          temWhatsApp = true;
          break;
      }
    }               

    if(temWhatsApp) {
        //abre whatsapp
        appActivity.startActivity(sendIntent);
    } else {
        //alerta - você deve ter o whatsapp instalado
        Toast.makeText(appActivity, appActivity.getString(R.string.share_whatsapp), Toast.LENGTH_SHORT).show();
    }
}
 0
Author: user3124158,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-21 05:28:04

Pobierz numer kontaktowy, do którego chcesz wysłać wiadomość i utworzyć uri dla whatsapp, tutaj c jest kursorem zwracającym wybrany kontakt.

Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp");           // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);
 0
Author: Kiran Maniya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-16 04:22:19

Jako Dokumentacja mówi, że możesz użyć adresu URL w stylu:

https://wa.me/15551234567

Gdzie ostatni segment jest liczbą w E164 Format

Uri uri = Uri.parse("https://wa.me/15551234567");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
 0
Author: GVillani82,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-13 11:30:18