Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android send SMS automatically on button click

I am trying to automatically send SMS message to a certain number when the user presses a button on the screen.

This is my code:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("sms:xxxxxxxxxxx")); 
smsIntent.putExtra("sms_body", "Hello");
startActivity(smsIntent);

xxxxxxx = phone number

I have the following permissions:

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

When I press the button it takes me to another screen where I can edit my text and press Send. I just want it to do this process automatically without taking me to another screen. As I've already defined my message, I just want to send it to a particular number.

And also I am not sure if I put the corrent phone number in the second line of code. Do I have to put my country code in there first or can I just put my mobile phone number and it will work?

Thank you

like image 757
Alex Avatar asked May 15 '12 19:05

Alex


People also ask

What is SmsManager in Android?

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) .

How can send SMS to multiple numbers in android programmatically?

You can't do this via Intent, as the android SMS app doesn't allow multiple recipients. You can try using the SmsManager class. First of all you need to request the permission android.


1 Answers

Try this code:

 String messageToSend = "this is a message";
 String number = "2121234567";

 SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);

With regards to the number, you need to enter the number as if you were calling it from the phone or sending an sms message in the normal manner.

like image 186
Chris Thompson Avatar answered Oct 12 '22 07:10

Chris Thompson