I'm developing an Android app where i need to send single message to
receive multiple emulator at a time .But the problem is only one
emulator is receiving the message.Here is my code.
public class SMS extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btn_SendSms);
txtPhoneNo = (EditText) findViewById(R.id.edittext_PhoneNumber);
txtMessage = (EditText) findViewById(R.id.edittext_MessageBody);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
String phoneNo = txtPhoneNo.getText().toString();
StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && message.trim().length()>0)
{
sendSMS(tempMobileNumber, message);
}
else
{
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver()
{
#Override
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
} , new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
To send an SMS message to another emulator instance which running on same machine, launch the SMS application. Specify the console port number(ex:5555) of the target emulator instance as as the SMS address.
Please note that this answer is based on :Linuxtopia guide.
You can create the array of emulator id or number then put sendTextMessage into that execute the loop as many element into the the array. OR You can have UI that allow the user to insert the phone number or emulator number in to the list and same procedure as above !!!
note that sendTextMessage `s first argument is "Phone Number" to whom you want to send sms
simply replace it with your requirement every as loop iterate
Related
This question already has answers here:
Show compose SMS view in Android
(13 answers)
Closed 5 years ago.
Hi I'm working on an app that upon the click of a button sends the location of the person to a phone number of their choice by SMS. I've little experience and have looked into using Twilio API. Does anyone know if this would work or is there another way to go about this? Getting the location is already covered I'm just looking at how I can now send this to a phone number on the click of a button. Thanks
So, from what i know, you don't really need a 3rd party to send the SMS from Android to a predefined phone number.
Please check out this post, where the problem is tackled : Send SMS in android
For ease of use, you can use/check this function, that worked for my projects :
*NOTE : this is for sending SMS to multiple phone numbers ( hence the for loop )
private void MultipleSMS(String phoneNumber, final String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
for (int i = 0; i < MobNumber.size() - 1; i++) {
values.put("address", MobNumber.get(i).toString());
// txtPhoneNo.getText().toString());
values.put("body", message.toString());
}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
doCall();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
I'm rather new to Android.
Im trying to send SMS from Android application.
When using the SMS Intent the SMS window opens and the user needs to approve the SMS and send it.
Is there a way to automatically send the SMS without the user confirming it?
Thanks,
Lior
You can use this method to send an sms. If the sms is greater than 160 character then sendMultipartTextMessage is used.
private void sendSms(String phonenumber,String message, boolean isBinary)
{
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
if(isBinary)
{
byte[] data = new byte[message.length()];
for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
{
data[index] = (byte)message.charAt(index);
}
manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
}
else
{
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH)
{
ArrayList<String> messagelist = manager.divideMessage(message);
manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
}
else
{
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
}
}
}
Update
piSend and piDelivered are Pending Intent They can trigger a broadcast when the method finish sending an SMS
Here is sample code for broadcast receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = null;
switch (getResultCode()) {
case Activity.RESULT_OK:
message = "Message sent!";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message = "Error. Message not sent.";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message = "Error: No service.";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message = "Error: Null PDU.";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message = "Error: Radio off.";
break;
}
AppMsg.makeText(SendMessagesWindow.this, message,
AppMsg.STYLE_CONFIRM).setLayoutGravity(Gravity.BOTTOM)
.show();
}
};
and you can register it using below line in your Activity
registerReceiver(receiver, new IntentFilter(SMS_SENT)); // SMS_SENT is a constant
Also don't forget to unregister broadcast in onDestroy
#Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
If your application has in the AndroidManifest.xml the following permission
<uses-permission android:name="android.permission.SEND_SMS"/>
you can send as many SMS as you want with
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(...);
and that is all.
Yes, you can send SMS using the SmsManager. Please keep in mind that your application will need the SEND_SMS permission for this to work.
Yes, you can send sms without making user interaction...But it works, when user wants to send sms only to a single number.
try {
SmsManager.getDefault().sendTextMessage(RecipientNumber, null,
"Hello SMS!", null, null);
} catch (Exception e) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(this);
AlertDialog dialog = alertDialogBuilder.create();
dialog.setMessage(e.getMessage());
dialog.show();
}
Also, add manifest permission....
<uses-permission android:name="android.permission.SEND_SMS"/>
i am developing android application.In my application i need to send sms to recipant.If sms is send to recipient it will show toast message and display new activity.I did using SMSManager class but i do not want to use SMSManager in my application.I want to use intent for send sms which gives me default SMS view for sending message. I used intent which send sms successfully but unable call other activity.Please solve my this problem i did lot of R&D but not successfull.
i am attaching source code of send sms and called activity
try {
String smsSent = "SMS_SENT";
String smsDelivered = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(smsSent), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(smsDelivered), 0);
// Receiver for Sent SMS.
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
Intent i=new Intent(this,Display.class).startActivity(i);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(smsSent));
// Receiver for Delivered SMS.
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(smsDelivered));
/*SmsManager smsman=SmsManager.getDefault();
smsman.sendTextMessage(no, null, data, null, null);*/
/*
// called new activity here
Intent i=new Intent(MainActivity.this,Display.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "SMS sent",Toast.LENGTH_LONG).show();
*/
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "");
smsIntent.putExtra("sms_body","");
startActivity(smsIntent);
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
You can use onActivityResult method.
edit your code as below:
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "");
smsIntent.putExtra("sms_body","");
startActivityForResult(smsIntent, MY_REQUEST_CODE);//MY_REQUEST_CODE int number
you can then override onActivityResult() and catch that request code (which is self defined in the starting activity) there. This is what it looks like:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// do something useful
}
}
}
I'm getting a NullPointerException when trying to send text messages to an invalid number (by invalid I mean phone numbers containing alphabetic characters etc., not an out-of-service number)
The code I used is from this tutorial:
http://mobiforge.com/developing/story/sms-messaging-android
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
Having the app crash when the user happens to enter an invalid phone number isn't the best. So my question is what would be a good approach for dealing with this?
I could catch NullPointerExceptions from this code and raise an error toast then, but it seems that NullPointerException is also raised on other errors, e.g. if the message is too long and should be multi-part.
Another hacky way would be to restrict the EditText to accept only digits and the '+' character.
I'm not happy with either of these solutions - it seems to me that ideally the sendTextMessage method should either raise a more informative exception or broadcast an error code for this?
Setting the input type of the edittext to phone numbers works well. Any combination of digits entered through the "phone number" input widget does not seem to raise the exception (e.g. "#*,()123")
In XML:
android:inputType="phone"
Thanks to awadm for pointing me in the right direction
I have a program that sends pre-defined text messages to a group of people at a push of a button. I have it working well but the problem I have is that when it sends the messages, it pops up with 2 toasts per message sent. Code:
package com.mfd.alerter;
//imports
public class homeScreen extends Activity {
//buttons
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//vars
// Grab the time
final Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("km");
final String formattedTime = formatter.format(anotherCurDate);
// Contacts
final String[] numbers = getResources().getStringArray(R.array.numbers);
// Start messages. Only 1 is given to shorten post
callStructureFire.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String msgText = "MFD PAGE OUT:\nStructure Fire\nTimeout:"+formattedTime;
for (int i = 0; i < numbers.length; i++) {
sendSMS(numbers[i], msgText);
}
}
});
//more call types. not important.
}
//---sends a SMS message to another device---
private void sendSMS(String numbers, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(numbers, null, message, sentPI, null);
}
//action bar stuff. not important.
}
More in detail: Lets say I send the text to 3 people, 6 toast messages will pop up saying "SMS Sent". How do I make it so only 3 will show up?
Also, Is there a way to maybe add a counter of the messages sent? Ex: "Message 1/10 sent", "Message 2/10 sent", etc?
I didn't really look at your code or asked myself why this happens but here's a trick to stop toasts show up twice:
Create a Toast instance using makeToast(), before showing it you call cancel(), set your text and then call show(). This will dismiss the previous toast. You won't even notice that a toast is displayed twice.
That's a stupid workaround, but it works for me ;-)
Aren't you supposed to register the receiver only once and not every time you call sendSMS.
You get 6 Toasts with three sms messages because you have 3 BroadCastReceivers. So in the first run you get 1 Toast. In the second run you get 2 Toasts (the receiver that was registered in the first run is called, and the one in the second). In the third run all three receivers are called, so you get three more toasts. All in sum - 6 Toasts...
so I guess, you have to register only one receiver before the for loop where you call sendSMS, or if you want the registration in sendSMS, then you have to unregister at the end of the method.
I hope this helps,
cheers!