Android Studio Send SMS
Syntax
To send an SMS message from an Android Studio app, use the following code snippet:
// set destination phone number and message text
String phoneNumber = "1234567890";
String message = "Hello, this is a test message.";
// create SMS intent
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:" + phoneNumber));
sendIntent.putExtra("sms_body", message);
// start SMS activity
startActivity(sendIntent);
Example
Here is an example of how to send an SMS message in Android Studio:
// set destination phone number and message text
String phoneNumber = "9876543210";
String message = "Hey, just wanted to say hi.";
// create SMS intent
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:" + phoneNumber));
sendIntent.putExtra("sms_body", message);
// start SMS activity
startActivity(sendIntent);
Output
The above code will open the default SMS app of the user with the phone number and message pre-filled in the compose message screen. The user can then edit the message if needed and send it to the desired recipient.
Explanation
The above code creates an instance of the Intent
class with the ACTION_VIEW
action and a Uri
with the phone number pre-filled in the SMS app. Then, the message text is added as an extra to the Intent
. Finally, the startActivity()
method is called on the Intent
to open the SMS app.
Use
This code snippet can be used to send SMS messages from any Android Studio app. It can be used to send informational messages to users, promotional messages to customers, and more.
Important Points
- Make sure to add the permission
android.permission.SEND_SMS
to the AndroidManifest.xml file. - The user must have an SMS app installed on their device for this code to work.
- The destination phone number must be in the format
sms:[phone number]
.
Summary
In this tutorial, we learned how to send SMS messages from an Android Studio app using the Intent
class. We also learned about the syntax, example, output, explanation, use, important points, and summary of the code.