Why does sendTextMessage require READ_PHONE_STATE permission?

My application sent this stack trace home, which seems like something very bad is happening under the hood.

phone_model = SKY IM-A630K, android_version = 2.1-update1

java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10089 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1218) at android.os.Parcel.readException(Parcel.java:1206) at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getLine1Number(IPhoneSubInfo.java:223) at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:764) at android.telephony.SmsManager.sendTextMessage(SmsManager.java:129) at android.telephony.SmsManager.sendTextMessage(SmsManager.java:108) at com.emergency.button.SMSSender.safeSendSMS(SMSSender.java:91) at com.emergency.button.EmergencyActivity$EmergencyThread.sendSMS(EmergencyActivity.java:294) at com.emergency.button.EmergencyActivity$EmergencyThread.sendMessages(EmergencyActivity.java:386) at com.emergency.button.EmergencyActivity$EmergencyThread.run(EmergencyActivity.java:266) 

So should I just catch any exceptions throughout sendTextMessage? Who is to blame?

+8
android permissions sms
source share
3 answers

My application sent this stack trace home, which seems like something very bad is happening under the hood.

As you say this, I assume this is the first time you have seen this problem, and this does not happen on other phones.

Is it possible that in the Android implementation on this phone model SmsManager tries to check the status of the phone before trying to send an SMS message. This is pure speculation, but it does not seem unreasonable, although the Telephony API docs for SMS Manager do not mention this. Maybe not, I'm not sure.

As sending an SMS is very important in your application, then yes, of course, you should do your best to catch any exceptions related to sending messae (and, if possible, recovering from an exception state).

It seems this particular problem cannot be repaired, why not declare the use-permission for READ_PHONE_STATE in your manifest?

+2
source share

Now I see that in Lollipop (API 21), even when using a benign function such as SmsManager.getDefault().divideMessage(String) , READ_PHONE_STATE permission is READ_PHONE_STATE . I am sure that this was not required before, and that this is a problem with the OS, since I tested it on Nexus 5 devices before and after upgrading to Lollipop. Previously, when KitKat started, SMS worked fine without the permission of READ_PHONE_STATE . And after that it was required.

The reason is that, I think, telephony features are trying to make wise decisions, well, that’s it. Thus, a simple task, for example, splitting SMS (not even sending), goes to SmsManager to request it about the status of the phone.

I think this is a design mistake. And, as you said above, this can and should scare users. Why do they have so many ambiguous permissions on Android?

This is my stack trace, just for fun:

 java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10078 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1540) at android.os.Parcel.readException(Parcel.java:1493) at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getGroupIdLevel1(IPhoneSubInfo.java:465) at android.telephony.TelephonyManager.getGroupIdLevel1(TelephonyManager.java:1666) at android.telephony.SmsMessage.hasEmsSupport(SmsMessage.java:776) at com.android.internal.telephony.gsm.SmsMessage.calculateLength(SmsMessage.java:808) at android.telephony.SmsMessage.fragmentText(SmsMessage.java:322) at android.telephony.SmsManager.divideMessage(SmsManager.java:328) at mobi.chatfish.utils.CFCommunications.sendSMSDirect(CFCommunications.java:138) 
+7
source share

I had the same problem with my HTC (Desire 728G) Dual Sim phone and I had to turn on "READ_PHONE_STATE", but now Google is asking for a privacy policy, but because I'm too lazy to do it :) I did some research and I found a better way without using "READ_PHONE_STATE". The problem is that some devices (mainly dual sims) require the permission "READ_PHONE_STATE" to search for the default "SubscriptionId", which happens when you call "SmsManager.getDefault ()". The following is the code I use to avoid this problem by assigning the value (1) SubscriptionId if any exception occurs:

  SmsManager smsManager = SmsManager.getDefault(); if (android.os.Build.VERSION.SDK_INT >= 22){ Log.e("Alert","Checking SubscriptionId"); try { Log.e("Alert","SubscriptionId is " + smsManager.getSubscriptionId()); } catch (Exception e) { Log.e("Alert",e.getMessage()); Log.e("Alert","Fixed SubscriptionId to 1"); smsManager = SmsManager.getSmsManagerForSubscriptionId(1); } } smsManager.sendTextMessage(mobileNumber, null, msgStr, null, null); 
0
source share

Source: https://habr.com/ru/post/650693/


All Articles