Send Whatsapp message to specific contact

前端 未结 8 2117
南旧
南旧 2020-12-05 05:27

I followed this link and this is my code

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(\"content://com.android.contacts/data/\" + \"MYNUMBER@s.whatsapp         


        
相关标签:
8条回答
  • 2020-12-05 06:15

    after googling a little, i found the following code

        public void onClickWhatsApp(View view) {
    
        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
                String text = "YOUR TEXT HERE";
        waIntent.setPackage("com.whatsapp");
        if (waIntent != null) {
            waIntent.putExtra(Intent.EXTRA_TEXT, text);//
            startActivity(Intent.createChooser(waIntent, "Share with"));
        } else {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }
    
    }
    

    so you can send an intent to send a message, but as far is ive read you cant send it to a specific contact

    0 讨论(0)
  • 2020-12-05 06:18

    I found the right way to do this and is just simple after you read this article: http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/

    phone and message are both String.

    Source code:

    try {
                PackageManager packageManager = context.getPackageManager();
                Intent i = new Intent(Intent.ACTION_VIEW);
                String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8");
                i.setPackage("com.whatsapp");
                i.setData(Uri.parse(url));
                if (i.resolveActivity(packageManager) != null) {
                    context.startActivity(i);
                }
            } catch (Exception e){
                e.printStackTrace();
            }
    

    Enjoy!

    0 讨论(0)
提交回复
热议问题