How can I send sms messages in the BACKGROUND using Android?

Viewed 24559

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?

4 Answers

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

    }
}
Related