-
Notifications
You must be signed in to change notification settings - Fork 8
/
android-M_permissions.slide
85 lines (57 loc) · 3.09 KB
/
android-M_permissions.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Android M permission
Louis Chan
Developer, Oursky
* Permission in Android M
Permission are now classified into 2 types.
- Normal: same as before, need not special handling
- Dangerous: must check before use
[[http://developer.android.com/guide/topics/security/permissions.html#perm-groups]]
* Permission group
Permission are granted as group.
e.g. If your app are granted [[http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE][WRITE_EXTERNAL_STORAGE]] permission, it is automatically granted [[http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE][READ_EXTERNAL_STORAGE]].
* int ContextCompat.checkSelfPermission(Context context, String permission)
Return `PackageManager.PERMISSION_DENIED` or `PackageManager.PERMISSION_GRANTED`.
* boolean ActivityCompat.shouldShowRequestPermissionRationale(Activity activity, String permission)
Return `false` if it is the first time asking for permission.
Return `true` if the request was denied *AND* "never ask again" was *NOT* checked.
Return `false` if the request was deined *AND* "never ask again" was checked.
* void ActivityCompat.requestPermissions(Activity activity, String[] permissions)
`activity` must implement `ActivityCompat.OnRequestPermissionResultCallback`.
Display a non-customizable dialog if "never ask again" was not checked.
Invoke the callback instantly otherwise.
* Example flow
// the snippet may reside in a OnClickListener
if (ContextCompat.checkSelfPermission(context, permission)) {
// process as normal
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// This implies the user has denied in the past
// the app should explain why the app need the permission in its own way
} else {
ActivityCompat.requestPermissions(activity, new String[]{permission});
}
}
* Example flow (cont.)
// the implementation of ActivityCompat.OnRequestPermissionResultCallback
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionResult(requestCode, permissions, grantResults);
if (requestCode != MY_REQUEST_CODE) {
return;
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// process as normal
} else if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// This implies request was deined but never ask again was not checked
} else {
// This implies the dialog actually did not show and this method was invoke instantly
// If the permission is essential to the app, the app should tell the user
// the app will not function properly without the permission
}
}
* Redirect to the app details in settings
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", YOUR_APP_PACKAGE_NAME, null);
intent.setData(uri);
startActitity(intent);
The above intent will open up the app details in settings but the user have to manually click to enter the permission page.