Android 6.0 - Permissions to send Text Messages (send SMS)

5

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 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 do not the permissions work? Should I request them for being Android 6.0?

    
asked by mrcaste 26.04.2016 в 01:54
source

1 answer

6

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

import android.telephony.gsm.SmsManager;

should be:

import android.telephony.SmsManager;
  

android.telephony.gsm.SmsManager is obsolete from API 4.   Superseded by android.telephony.SmsManager that 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

is required to guarantee 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!");
    }
}
    
answered by 26.04.2016 / 03:13
source