java – Permissions to send Text Messages (send SMS)

Question:

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(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:813)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
at com.globalsiag.sms_global.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:88)
at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

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

Answer:

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 Stackoverflow.com";
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