android.telephony.SmsManager. Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method getDefault() . To create an instance of SmsManager associated with a specific subscription ID, call getSmsManagerForSubscriptionId(int) .
In some versions of Android, this permission is turned on by default. In other versions, this permission is turned off by default. To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.
From the Home screen, tap the Apps icon (in the QuickTap bar) > the Apps tab (if necessary) > Tools folder > Messaging .
You can use the following code:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
+ phoneNumber)));
Make sure you set phoneNumber
to the phone number that you want to send the message to
You can add a message to the SMS with (from comments):
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
This worked for me.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendSMS("5556", "Hi You got a message!");
/*here i can send message to emulator 5556. In Real device
*you can change number*/
}
});
}
//Sends an SMS message to another device
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
You can add this line in AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
Take a look at this
This may be helpful for you.
Try the below code and then call, sendSMS("99999999999", "message");
to send sms in desired number.
//---sends an SMS message to another device---
@SuppressWarnings("deprecation")
private void sendSMS(String phoneNumber, String message)
{
Log.v("phoneNumber",phoneNumber);
Log.v("MEssage",message);
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, **DummyClasshere.class**), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
Please place the following permission into AndroidManifest.xml
file.
<uses-permission android:name="android.permission.SEND_SMS"/>
Hope this might help.
Update From the comment: DummyClasshere.class is an activity without doing any process or the class in which u need to navigate.
You can use Object.class in place of DummyClasshere.class.
This will definitely work, In this, Send message without using any intent .
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("Phone Number", null, "Message", null, null);
This code is used for send message in background (Not showing message composer), It can also work inside the Broadcast receiver. If you want to send a message from Broadcast receiver.
<uses-permission android:name="android.permission.SEND_SMS"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With