My app uses "dangerous" permissions such as READ_CONTACTS
, among others. All the examples I have seen to support this new model use classes derived from AppCompatActivity
. Do I really have to change my activities that subclass Activity to now subclass AppCompatActivity
? Is there an alternative way of doing this while still using activities? When I make that change my app crashes.
-
1Can you post your error here.? – Ronak Joshi Apr 05 '16 at 11:54
-
http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission/34722591#34722591 I Explain simple..... – Dixit Panchal Apr 05 '16 at 11:57
-
I explain Simple at Below Link. http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission/34722591#34722591 – Dixit Panchal Apr 05 '16 at 12:07
-
you can use google library for permission. I have explained here. http://stackoverflow.com/a/41893773/1429832 – Milon Jan 27 '17 at 12:18
1 Answers
Do I really have to change my activities that subclass Activity to now subclass AppCompatActivity?
No.
Is there an alternative way of doing this while still using activities?
It's the same way. There is nothing in AppCompatActivity
that has anything to do with runtime permissions. Whether you are inheriting from Activity
, AppCompatActivity
, or PhilodendronActivity
, you:
Call
checkSelfPermission()
(on yourContext
or thestatic
edition onContextCompat
) to see if you hold the permissionCall
requestPermissions()
(on yourActivity
or thestatic
edition onActivityCompat
) to request the permissions from the userImplement
onRequestPermissionsResult()
on yourActivity
to get the result of therequestPermissions()
call
So, for example, none of these sample apps use AppCompatActivity
:
An app just there to explore runtime permissions
The results of completing a hands-on tutorial to experiment with adding runtime permissions
Two apps that show requesting runtime permissions as part of setting up Play Services' fused location provider
An app that sets up runtime permissions for Maps V2 use

- 986,068
- 189
- 2,389
- 2,491
-
Thank you very much, I'll take a look shortly. Is it best to ask for all permissions at once, or one at a time, as dictated by user action? Read contacts is required at the beginning but write external storage may never be required if user doesn't take an action that requires it? – Phil O Apr 05 '16 at 12:22
-
@PhilO: Usually, you wait on requesting the permission until the user action, so you ask for as few permissions as possible. If the permission might not make sense to the user in the context of the user action, and you think that having some sort of "welcome" onboarding activity and asking for the permission up front might smooth things for the user, that's reasonable. – CommonsWare Apr 05 '16 at 12:27