Search code examples
androidruntime-permissions

Permission Group not working


After granted android.permission.RECEIVE_SMS by user, my app will read user's sms,
but some users will crash(i can't make it happened).

Both android.permission.RECEIVE_SMS and android.permission.READ_SMS are declared in AndroidManifest.xml, and they are same permission group(https://developer.android.com/guide/topics/security/permissions.html)

The detail crash report :

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    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)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/ from pid=24160, uid=10244 requires android.permission.READ_SMS, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
    at android.content.ContentResolver.query(ContentResolver.java:502)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    ... 3 more
java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/ from pid=24160, uid=10244 requires android.permission.READ_SMS, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
    at android.content.ContentResolver.query(ContentResolver.java:502)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    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)

I need help, Thanks


Solution

  • That's not quite how permission groups work. Groups are mainly for how permission requests are displayed to the user.

    When requesting a specific permission, the group description is what is presented to the user, not the specific permission description. Furthermore, if the user has already granted a specific permission in a group, a subsequent request for another permission in that group will automatically be granted, without having to prompt the user again.

    However, your app still needs to request each specific permission it needs. In your case, having the RECEIVE_SMS permission does not automatically grant your app the READ_SMS permission until you specifically request it.

    As noted on the developer page covering Requesting Permissions at Run Time:

    Note: Your app still needs to explicitly request every permission it needs, even if the user has already granted another permission in the same group. In addition, the grouping of permissions into groups may change in future Android releases. Your code should not rely on the assumption that particular permissions are or are not in the same group.