Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to send messages on WhatsApp on Android

If you want to send messages on WhatsApp from an Android app, you can use the WhatsApp API and the Intent system of Android. Here's how you can do this:

1. Sending a Text Message to a Specific Contact:

To send a message to a specific contact, you can use their phone number. However, the recipient must have your phone number saved in their contacts for the message to be delivered successfully.

String phoneNumber = "1234567890"; // Replace with the recipient's phone number
String message = "Hello from my app!";
Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + phoneNumber + "&text=" + URLEncoder.encode(message, "UTF-8"));

Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(sendIntent);

2. Sending a Message without Specifying a Contact:

If you don't specify a contact, WhatsApp will open and allow the user to select a contact from their list.

String message = "Hello from my app!";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setPackage("com.whatsapp");
sendIntent.setData(Uri.parse("http://api.whatsapp.com/send?text=" + URLEncoder.encode(message, "UTF-8")));
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
} else {
    Toast.makeText(this, "WhatsApp not installed.", Toast.LENGTH_SHORT).show();
}

3. Sending Media or Documents:

To send media or documents, you can use Android's ContentProvider and Intent. Here's an example using an image:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/*");
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///path/to/image.jpg")); // Replace with the path to your image or document
startActivity(sendIntent);

Note:

  • Ensure you have proper permissions to access and share content if you're using the device's storage.
  • WhatsApp has its own terms and conditions for third-party integrations. It's important to review the WhatsApp Business API documentation if you're planning to incorporate such functionalities in a commercial application.
  • Always make sure you're respecting privacy and obtaining user consent when sharing information via platforms like WhatsApp.
  1. How to use WhatsApp messaging on Android:

    • Use an Intent to open WhatsApp with pre-filled content:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  2. Android WhatsApp message sending example:

    • An example of sending a message on WhatsApp from an Android app:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  3. Sending text messages on WhatsApp from Android app:

    • Send text messages on WhatsApp using an Intent:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  4. Android WhatsApp integration for messaging:

    • Integrate WhatsApp messaging in Android by using an Intent:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  5. Implementing WhatsApp messaging in Android app:

    • Implement WhatsApp messaging in your Android app using the Intent method:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  6. Using Intent to send messages on WhatsApp in Android:

    • Use Intent to send messages on WhatsApp in Android:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }
    
  7. Android development: sending messages via WhatsApp:

    • Code example for sending messages via WhatsApp in Android development:
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a WhatsApp message!");
    
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        // Handle case where WhatsApp is not installed
    }