2

I am getting the crash below at this code line:

pkgMrg_global = getActivity().getPackageManager();
apps = pkgMrg_global.getInstalledApplications(0);
//GETTING CRASH AT LINE BELOW
Collections.sort(apps,
                        new ApplicationInfo.DisplayNameComparator(
                                pkgMrg_global));

What I dont understand is how Collections.sort leads to this Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10157 nor current process has android.permission.READ_PHONE_STATE?

Stacktrace:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10157 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getSubscriberId(IPhoneSubInfo.java:231)
at android.telephony.TelephonyManager.getSubscriberId(TelephonyManager.java:1352)
at android.app.ApplicationPackageManager.getSimOperatorNameForStk(ApplicationPackageManager.java:1548)
at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:1109)
at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:115)
at android.app.ApplicationPackageManager.getApplicationLabel(ApplicationPackageManager.java:1186)
at android.content.pm.ApplicationInfo$DisplayNameComparator.compare(ApplicationInfo.java:561)
at android.content.pm.ApplicationInfo$DisplayNameComparator.compare(ApplicationInfo.java:554)
at java.util.TimSort.binarySort(TimSort.java:261)
at java.util.TimSort.sort(TimSort.java:204)
at java.util.TimSort.sort(TimSort.java:169)
at java.util.Arrays.sort(Arrays.java:2010)
at java.util.Collections.sort(Collections.java:1883)
at com.mavdev.focusoutfacebook.fragments.addablock.apps.Fragment_appsselect_addblock$loadAppsListfromSystem.doInBackground(Fragment_appsselect_addblock.java:1830)
at com.mavdev.focusoutfacebook.fragments.addablock.apps.Fragment_appsselect_addblock$loadAppsListfromSystem.doInBackground(Fragment_appsselect_addblock.java:1802)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
user1406716
  • 9,565
  • 22
  • 96
  • 151
  • add that permission on your manifest file. – MetaSnarf Sep 21 '15 at 02:24
  • the cause of the error is either `.getInstalledApplications(0);` or new `ApplicationInfo.DisplayNameComparator(pkgMrg_global)` on your code – MetaSnarf Sep 21 '15 at 02:25
  • Yes but I don't understand why I have to. I am not using any phone function (just sorting that apps list) so shouldn't be needing it. Got this report from a user's phone so can't reproduce myself. – user1406716 Sep 21 '15 at 02:26
  • I tried running your code without read permission.there seems to be no problem. That's odd. These may have something to do with that user's device settings. – MetaSnarf Sep 21 '15 at 02:40
  • @user1406716 Do you know what kind of device / OS version. I'm thinking custom ROM. – Morrison Chang Sep 21 '15 at 03:07
  • 1
    I can confirm the error. This looks like a bug in Android. I also have an app requesting other apps labels without any deal to phone state, and don't want to add this permission. It's ridiculous that app labels are somehow bound to phone state. – Stan Oct 25 '15 at 16:25

2 Answers2

1

As per google docs here The note says Note: If both your minSdkVersion and targetSdkVersion values are set to 3 or lower, the system implicitly grants your app this permission. If you don't need this permission, be sure your targetSdkVersion is 4 or higher.

Protection level: dangerous

if so you need to add the permission

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

Constant Value: "android.permission.READ_PHONE_STATE"

Vaibhav Barad
  • 625
  • 8
  • 17
0

add this permission to your android manifest

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

Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41