java – Permissions to send Text Messages (send SMS)


Hello I am trying to send text messages from my app and using a physical device with Android 6.0 (Moto G 3rd generation), this is the code I use:

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("12345678" , null,"hello world" , null, null);

But when executing it, it throws me this exception:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.globalsiag.sms_global, PID: 22364
java.lang.SecurityException: Sending SMS message: uid 10113 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(
at android.os.Parcel.readException(
at android.telephony.SmsManager.sendTextMessageInternal(
at android.telephony.SmsManager.sendTextMessage(
at com.globalsiag.sms_global.MyGcmListenerService.onMessageReceived(
at Source)
at Source)
at Source)
at Source)
at$ Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$

Why don't the permissions work? Should I request them because it is Android 6.0?


It seems to me that according to the error message, the problem is because you are using the wrong class:

import android.telephony.gsm.SmsManager;

must be:

import android.telephony.SmsManager;

android.telephony.gsm.SmsManager is deprecated from API 4. Replaced by android.telephony.SmsManager which supports GSM and CDMA.

How to send an SMS message.

You only need to specify the permission in your AndroidManifest.xml

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

This is an example:

String phone = "1234567890";
String text = "Hi from";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, text , null, null);

For Android 6.0

It is required to guarantee the permission, for this you can use the following method:

private void checkSMSStatePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.SEND_SMS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para enviar SMS.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para enviar SMS!");
Scroll to Top