📌  相关文章
📜  如何在 Android 的 WhatsApp 上发送消息

📅  最后修改于: 2022-05-13 01:55:44.452000             🧑  作者: Mango

如何在 Android 的 WhatsApp 上发送消息

Whatsapp是最受欢迎的消息应用程序之一。许多安卓应用程序需要将一些消息直接从他们的应用程序共享到 WhatsApp 的功能。例如,如果用户想要共享应用程序或共享来自应用程序的消息,则使用此功能。用户可以发送文本或预定义文本也可以通过它发送。本文演示了 android 应用程序如何在 WhatsApp 上发送消息。 Whatsapp 必须安装在用户的设备上。

方法

第 1 步:打开activity_main.xml文件并添加布局代码。添加了一个作为 EditText 的消息输入容器和一个用于发送此消息的 Button。

activity_main.xml


  
   
   
  
   
   


MainActivity.java
package com.gfg;
  
import androidx.appcompat
    .app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity
    extends AppCompatActivity {
  
    @Override
    protected void onCreate(
        Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Taking reference of Edit Text
        final EditText messageEditText
            = findViewById(R.id.message);
  
        // Taking reference to button
        Button submit
            = findViewById(R.id.submit);
  
        submit.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
  
                    // Getting the text
                    // from edit text
                    String message
                        = messageEditText
                              .getText()
                              .toString();
  
                    // Calling the function
                    // to send message
                    sendMessage(message);
                }
            });
    }
  
    private void sendMessage(String message)
    {
  
        // Creating new intent
        Intent intent
            = new Intent(
                Intent.ACTION_SEND);
  
        intent.setType("text/plain");
        intent.setPackage("com.whatsapp");
  
        // Give your message here
        intent.putExtra(
            Intent.EXTRA_TEXT,
            message);
  
        // Checking whether Whatsapp
        // is installed or not
        if (intent
                .resolveActivity(
                    getPackageManager())
            == null) {
            Toast.makeText(
                     this,
                     "Please install whatsapp first.",
                     Toast.LENGTH_SHORT)
                .show();
            return;
        }
  
        // Starting Whatsapp
        startActivity(intent);
    }
}


步骤 2:在Java文件中引用 EditText 和 Button。在 findViewById() 方法的帮助下使用 id 获取引用。

第 3 步:编写函数以向 whatsapp 发送消息。使用ACTION_SEND创建一个意图,并为此指定 whatsapp 包名称,以便它直接打开 whatsapp。

private void sendMessage(String message)
{
  
    // Creating new intent
    Intent intent
        = new Intent(Intent.ACTION_SEND);
  
    intent.setType("text/plain");
    intent.setPackage("com.whatsapp");
  
    // Give your message here
    intent.putExtra(
        Intent.EXTRA_TEXT,
        message);
  
    // Checking whether Whatsapp
    // is installed or not
    if (intent
            .resolveActivity(
                getPackageManager())
        == null) {
        Toast.makeText(
                 this,
                 "Please install whatsapp first.",
                 Toast.LENGTH_SHORT)
            .show();
        return;
    }
  
    // Starting Whatsapp
    startActivity(intent);
}

第 4 步:onClickListener设置为按钮。它接受用户输入的文本并调用函数sendMessage ,在该函数中将文本消息作为参数发送。

submit.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
  
            // Getting the text
            // from edit text
            String message
                = messageEditText
                      .getText()
                      .toString();
  
            // Calling the function
            // to send message
            sendMessage(message);
        }
    });

下面是完整的MainActivity。 Java文件:

主要活动。Java

package com.gfg;
  
import androidx.appcompat
    .app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity
    extends AppCompatActivity {
  
    @Override
    protected void onCreate(
        Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Taking reference of Edit Text
        final EditText messageEditText
            = findViewById(R.id.message);
  
        // Taking reference to button
        Button submit
            = findViewById(R.id.submit);
  
        submit.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
  
                    // Getting the text
                    // from edit text
                    String message
                        = messageEditText
                              .getText()
                              .toString();
  
                    // Calling the function
                    // to send message
                    sendMessage(message);
                }
            });
    }
  
    private void sendMessage(String message)
    {
  
        // Creating new intent
        Intent intent
            = new Intent(
                Intent.ACTION_SEND);
  
        intent.setType("text/plain");
        intent.setPackage("com.whatsapp");
  
        // Give your message here
        intent.putExtra(
            Intent.EXTRA_TEXT,
            message);
  
        // Checking whether Whatsapp
        // is installed or not
        if (intent
                .resolveActivity(
                    getPackageManager())
            == null) {
            Toast.makeText(
                     this,
                     "Please install whatsapp first.",
                     Toast.LENGTH_SHORT)
                .show();
            return;
        }
  
        // Starting Whatsapp
        startActivity(intent);
    }
}

输出: