15

I am coming from iphone development where you cannot send an SMS in the background without asking the user to confirm the send. Can sms be sent in the background in android so that no user intervention is need?

Saravanan
  • 363
  • 1
  • 14
yazz.com
  • 57,320
  • 66
  • 234
  • 385
  • 4
    Check out this one http://stackoverflow.com/questions/5671564/how-to-send-sms-message-on-android-device-in-the-background – sealz Jun 15 '11 at 17:02

3 Answers3

28

Send SMS with SMS-Delivery notification as toast.

method call as below.

sendSMS("98********","This is test message");

method signature as below — using code that I copied from https://mobiforge.com/design-development/sms-messaging-android

/*
* BroadcastReceiver mBrSend; BroadcastReceiver mBrReceive;
*/
private void sendSMS(String phoneNumber, String message) {
  ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
  ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
  PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
          new Intent(mContext, SmsSentReceiver.class), 0);
  PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
          new Intent(mContext, SmsDeliveredReceiver.class), 0);
  try {
      SmsManager sms = SmsManager.getDefault();
      ArrayList<String> mSMSMessage = sms.divideMessage(message);
      for (int i = 0; i < mSMSMessage.size(); i++) {
          sentPendingIntents.add(i, sentPI);
          deliveredPendingIntents.add(i, deliveredPI);
      }
      sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
              sentPendingIntents, deliveredPendingIntents);

  } catch (Exception e) {

      e.printStackTrace();
      Toast.makeText(getBaseContext(), "SMS sending failed...",Toast.LENGTH_SHORT).show();
  }

}

Now two more classes SmsDeliveredReceiver,SmsSentReceiver as below.

public class SmsDeliveredReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent arg1) {
      switch (getResultCode()) {
          case Activity.RESULT_OK:
              Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
              break;
          case Activity.RESULT_CANCELED:
              Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
              break;
      }
   }
}

Now SmsSentReceiver.

public class SmsSentReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent arg1) {
      switch (getResultCode()) {
          case Activity.RESULT_OK:
              Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
              break;
          case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
              Toast.makeText(context, "SMS generic failure", Toast.LENGTH_SHORT).show();
              break;
          case SmsManager.RESULT_ERROR_NO_SERVICE:
              Toast.makeText(context, "SMS no service", Toast.LENGTH_SHORT)
              .show();
              break;
          case SmsManager.RESULT_ERROR_NULL_PDU:
              Toast.makeText(context, "SMS null PDU", Toast.LENGTH_SHORT).show();
              break;
          case SmsManager.RESULT_ERROR_RADIO_OFF:
              Toast.makeText(context, "SMS radio off", Toast.LENGTH_SHORT).show();
              break;
      }
   }
}

Now Permissions open your AndroidManifest.xml and add below line

<uses-permission android:name="android.permission.SEND_SMS"/>
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • Whats are the intent filter for these broadcast receivers in manifest? – Saty Jul 28 '14 at 08:10
  • @Saty Here are the broadcast receivers to send sms . You can define it in your manifest file. – GrIsHu Aug 17 '16 at 11:34
  • 1
    it works without two BroadCastReceiver classes. so pending intens and so on.. can use instead: sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,null,null); Thank you very much @sandy; – Samir Alakbarov Jun 02 '20 at 18:18
20

Yes, you can do it by using:

SmsManager sm = SmsManager.getDefault(); 
sm.sendTextMessage(number, null, message, null, null); 
Shubham Gupta
  • 1,123
  • 11
  • 16
sealz
  • 5,348
  • 5
  • 40
  • 70
1

Best answer is good but above API level 23 you will need to get the permission pragmatically. Otherwise permission will be prompted each time.

 private static final int PERMISSION_REQUEST_CODE = 1;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

    if (checkSelfPermission(Manifest.permission.SEND_SMS)
            == PackageManager.PERMISSION_DENIED) {

        Log.d("permission", "permission denied to SEND_SMS - requesting it");
        String[] permissions = {Manifest.permission.SEND_SMS};

        requestPermissions(permissions, PERMISSION_REQUEST_CODE);

    }
}
Khizhny Andrey
  • 161
  • 1
  • 5