Why does SmsManager require READ_PHONE_STATE permission on some devices but not others?
The app uses SmsManager to send SMS text messages. The next method is called only after the user's SEND_SMS permission has been successfully obtained from the user. The app targets API 25.
public static void sendSmsTextMessage(Context context, String number, String message) {
SmsManager sms = SmsManager.getDefault();
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS);
if (permissionCheck == PERMISSION_DENIED) {
Timber.e("Permission to send SMS denied");
} else {
sms.sendTextMessage(number, null, message, null, null);
}
}
So far, it has worked fine on all devices it has been tested on. But now it is used on Logicom L-EMENT 553 phone and the app explodes when trying to call sendTextMessage () on API 23 (Marshmallow) with this exception:
Exception java.lang.RuntimeException: Failure delivering result
ResultInfo{who=@android:requestPermissions:, request=1, result=-1,
data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has
extras) }} to activity
{com.myapp.android/com.myapp.android.ui.bet.BetActivity}:
java.lang.SecurityException: Neither user 10108 nor current process
has android.permission.READ_PHONE_STATE.
Why was READ_PHONE_STATE permission required on some devices but not others when sending SMS messages? Obviously, it is preferable not to ask for this permission, as it is a big user request to grant this level of access.
The answer is here Why sendTextMessage requires READ_PHONE_STATE permission? suggests some Android versions contain a bug asking for this permission, but is it that sendTextMessage () was passed a length limit message and then split into it using divideMessage (), which then asks for this permission? Even though I don't see any evidence, sendMessage () automatically splits long messages into smaller chunks and sends them.
source to share
I think because some manufacturers have fixed a bug in Android. I tried this in an emulator (Android 8.0, API 26) and it throws an exception on sendSmsTextMessage (). So I did a workaround like this:
try {
SmsManager.getDefault().sendTextMessage(msisdn, null, text, null, null);
return false;
} catch (Exception e) {
if (e.toString().contains(Manifest.permission.READ_PHONE_STATE) && ContextCompat
.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)!=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission
.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
return true;
} // else it some other exception
}
... and in the declaration:
<uses-permission-sdk-23 android:maxSdkVersion="26"
android:name="android.permission.READ_PHONE_STATE"/>
... because this seems to be solved in API 27, at least it doesn't happen in the emulator with API 27.
source to share