Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send sms using SMSManager in Android

In my application I do not want to use the default message sender. For doing that I followed the following link In Android is it possible to send sms message to more than one recipient in code?

  • And that code worked too. But the messages I am sending from this code are not saved on the phones outbox and inbox.
  • I am using sms manager like this in my code

    SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null);

But it is not sending sms.please help me with how can i send sms in android - i have tried following too PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent( SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

It's also not working.
SMSAPPActivity.java

EDIT :

btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message = txtMessage.getText().toString();
                String[] PhnNoArray = new String[2];
                PhnNoArray[0] = "9999999999";
                PhnNoArray[1] = "8888888888";
                // StringTokenizer st = new StringTokenizer(phoneNo, ",");
                smsManager = SmsManager.getDefault();
                for (int i = 0; i < PhnNoArray.length; i++) {
                    smsManager = SmsManager.getDefault();
                        // this is the function that does all the magic
//                      sms.sendTextMessage(phoneNumber, null, msg, pi, null);
                    smsManager.sendTextMessage(PhnNoArray[i], null, message, null,
                            null);
                    Toast.makeText(getBaseContext(), "SMS sent : " + i,
                            Toast.LENGTH_SHORT).show();
                }
}
        });

Please see the edit and tell me what i have done wrong.tost is showing up but sms is not received on other phone by using this code

like image 294
Shruti Avatar asked Jan 25 '12 06:01

Shruti


People also ask

Can Android Studio emulator send SMS?

You can emulate sending SMS messages to the Android emulator using either the Dalvik Debug Monitor Service (DDMS) tool available in Eclipse, or the Telnet client.

Can you send texts from Android emulator?

If you read the emulator documentation, you will see that you can send and receive text messages between the environment within the emulator and the emulator console.

How do I allow an app to send SMS?

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.


1 Answers

I used following code to send sms to multiple numbers and sent sms gets saved in messages

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:
                    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", MessageText.getText().toString());
                    }
                    getContentResolver().insert(
                            Uri.parse("content://sms/sent"), values);
                    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);
    }

to send message to multiple numbers i used above function as :

if (MobNumber != null) {

                for (int i = 0; i < MobNumber.size(); i++) {
                    String message = MessageText.getText().toString();
                    String tempMobileNumber = MobNumber.get(i).toString();
                    sendSMS(tempMobileNumber, message);
                }
like image 179
Shruti Avatar answered Sep 19 '22 14:09

Shruti