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
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:
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);
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(); }
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);
How to use WhatsApp messaging on Android:
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 }
Android WhatsApp message sending example:
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 }
Sending text messages on WhatsApp from Android app:
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 }
Android WhatsApp integration for messaging:
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 }
Implementing WhatsApp messaging in Android app:
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 }
Using Intent to send messages on WhatsApp in Android:
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 }
Android development: sending messages via WhatsApp:
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 }