Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีส่งข้อความผ่าน WhatsApp ใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีส่งข้อความผ่าน WhApp โดยใช้ Intent.ACTION_SEND และระบุ Package Name ของ WhatsApp

ขั้นตอนการสร้าง

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send WhatsApp Message"
        android:textSize="30sp"
        android:gravity="center"
        android:padding="16dp" />
</LinearLayout>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.text);
        textView.setOnClickListener(v -> sendWhatsAppMessage());
    }

    private void sendWhatsAppMessage() {
        PackageManager pm = getPackageManager();
        
        try {
            // ตรวจสอบว่า WhatsApp ติดตั้งอยู่
            PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
            
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.setPackage("com.whatsapp"); // ระบุ Package เพื่อเปิด WhatsApp โดยตรง
            intent.putExtra(Intent.EXTRA_TEXT, "Hello from my Android App!");
            
            startActivity(Intent.createChooser(intent, "Share with"));
            
        } catch (PackageManager.NameNotFoundException e) {
            Toast.makeText(this, "WhatsApp not installed", Toast.LENGTH_SHORT).show();
            
            // นำไปดาวน์โหลดที่ Play Store
            // openPlayStore("com.whatsapp");
        }
    }
    
    // ฟังก์ชันเสริม: เปิด Play Store
    private void openPlayStore(String packageName) {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, 
                Uri.parse("market://details?id=" + packageName)));
        } catch (ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW, 
                Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
        }
    }
}

การส่งข้อความไปยังเบอร์/กลุ่มเฉพาะ

WhatsApp รองรับ URI Scheme สำหรับเปิดแชทโดยตรง:

1. เปิดแชทกับเบอร์โทรศัพท์ (ต้องเป็นรูปแบบ International)

// ใช้ Intent.ACTION_VIEW กับ whatsapp:// URI
String phoneNumber = "66812345678"; // รหัสประเทศไทย 66 + เบอร์ไม่มี 0 นำหน้า
String url = "https://wa.me/" + phoneNumber + "?text=" + Uri.encode("Hello!");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

2. ส่งข้อความพร้อมรูปภาพ/ไฟล์

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_STREAM, imageUri); // Uri ของรูปภาพ
intent.putExtra(Intent.EXTRA_TEXT, "Caption here");
startActivity(intent);

3. แชร์หลายไฟล์

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.setPackage("com.whatsapp");
ArrayList<Uri> uris = new ArrayList<>();
uris.add(imageUri1);
uris.add(imageUri2);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(intent);

ข้อควรระวัง

  • Android 11+ (API 30): ต้องเพิ่ม <queries> ใน Manifest
  • WhatsApp Business ใช้ Package Name: com.whatsapp.w4b
  • ผู้ใช้ต้องมี WhatsApp ติดตั้งและล็อกอินแล้ว
  • ไม่สามารถส่งข้อความโดยอัตโนมัติโดยไม่มีการโต้ตอบจากผู้ใช้ (Policy ของ WhatsApp)

Manifest สำหรับ Android 11+

<manifest ...>
    <queries>
        <package android:name="com.whatsapp" />
        <package android:name="com.whatsapp.w4b" />
    </queries>
    <application ...>...</application>
</manifest>

ทางเลือก: WhatsApp Business API

สำหรับการส่งข้อความอัตโนมัติ/ระบบ (ไม่ต้องการ UI) ต้องใช้ WhatsApp Business API ซึ่งเป็นบริการเสียค่าใช้จ่ายและต้องสมัครผ่าน BSP (Business Solution Provider)