Table 1. Permissions and permission groups.
Permission Group | Permissions |
---|---|
android.permission-group.CALENDAR |
|
android.permission-group.CAMERA |
|
android.permission-group.CONTACTS |
|
android.permission-group.LOCATION |
|
android.permission-group.MICROPHONE |
|
android.permission-group.PHONE |
|
android.permission-group.SENSORS |
|
android.permission-group.SMS |
|
android.permission-group.STORAGE |
|
requestPermissions()
.,提示用户,
让用户不会感到困惑
ActivityCompat.OnRequestPermissionsResultCallback
PermissionChecker
class, which provides several static utility methods for apps that use IPC to provide services for other apps. For example,
PermissionChecker.checkCallingPermission()
checks whether an IPC made by a particular package has a specified permission.
requestPermissions()
的一些说明:
requestPermissions()
method, the system shows a standard dialog box to the user.
requestPermissions()
, as described in Explain why the app needs permissions
.
requestPermissions()
时,系统会显示一个获取权限的提示对话框,当前应用不能配置和修改这个对话框,
requestPermissions()
之前处理。
shouldShowRequestPermissionRationale()
的一些说明:
To help find the situations where you need to provide extra explanation, the system provides theshouldShowRequestPermissionRationale()
method.
This method returns true
if the app has requested this permission previously and the user denied the request.
That indicates that you should probably explain to the user why you need the permission.
If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false
.
The method also returns false
if the device policy prohibits the app from having that permission.
1. 第一次请求权限时,用户拒绝了,下一次:shouldShowRequestPermissionRationale()
返回 true,应该显示一些为什么需要这个权限的说明
2.第二次请求权限时,用户拒绝了,并选择了“不在提醒”的选项时:shouldShowRequestPermissionRationale()
返回 false
3. 设备的策略禁止当前应用获取这个权限的授权:shouldShowRequestPermissionRationale()
返回 false
注意:上面的:第二次请求权限时,才会有“不在提醒”的选项,如果用户一直拒绝,并没有选择“不在提醒”的选项,下次请求权限时,会继续有“不在提醒”的选项
shouldShowRequestPermissionRationale()
的方法说明:
Gets whether you should show UI with rationale for requesting a permission.
You should do this only if you do not have the permission and the context in which the permission is requested does not clearly communicate to the user what would be the benefit from granting this permission.
For example, if you write a camera app, requesting the camera permission would be expected by the user and no rationale for why it is requested is needed. If however, the app needs location for tagging photos then a non-tech savvy user may wonder how location is related to taking photos. In this case you may choose to show UI with rationale of requesting this permission.
根据方法说明:
显示权限说明:是根据你的应用中使用的权限分类来的:
1.用户容易知道应用需要获取的权限:如一个拍照应用,需要摄像头的权限,是很正常,不用提示。
2.一些用户感觉困惑的一些权限:如:分享图片,还需要获取位置的权限,这个需要提示用户:为什么需要这个权限。
参考:
如果用户拒绝某授权。下一次弹框,用户会有一个“不再提醒”的选项的来防止app以后继续请求授权。
如果这个选项在拒绝授权前被用户勾选了。下次为这个权限请求requestPermissions时,对话框就不弹出来了,结果就是,app啥都不干。 我们需要检查是否需要展示请求权限的提示通过activity的shouldShowRequestPermissionRationale,代码如下:
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
private void insertDummyContactWrapper() {
int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_CONTACTS);
if
(hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
//TODO
if
(!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {
showMessageOKCancel(
"You need to allow access to Contacts"
,
new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(
new
String[] {Manifest.permission.WRITE_CONTACTS},
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return
;
}
requestPermissions(
new
String[] {Manifest.permission.WRITE_CONTACTS},
REQUEST_CODE_ASK_PERMISSIONS);
return
;
//TODO
}
insertDummyContact();
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new
AlertDialog.Builder(MainActivity.
this
)
.setMessage(message)
.setPositiveButton(
"OK"
, okListener)
.setNegativeButton(
"Cancel"
,
null
)
.create()
.show();
}
|
Currently, the following permissions are classified as PROTECTION_NORMAL
:
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE(新文档没有)
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE(新文档没有)
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
(新文档没有)
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
Table 1. Permissions and permission groups.
Permission Group | Permissions |
---|---|
android.permission-group.CALENDAR |
|
android.permission-group.CAMERA |
|
android.permission-group.CONTACTS |
|
android.permission-group.LOCATION |
|
android.permission-group.MICROPHONE |
|
android.permission-group.PHONE |
|
android.permission-group.SENSORS |
|
android.permission-group.SMS |
|
android.permission-group.STORAGE |
|
$ adb shell pm list permissions -d -g
$ adb shell pm [grant|revoke] <permission-name> ...
adb -s emulator-5554 shell pm list permissions -d -g :列出设备emulator-5554上所有的分组危险权限(-d:表示危险权限 -g:表示分组)
adb -s emulator-5554 shell pm list permissions -g :列出设备emulator-5554上所有的分组权限(-g:表示分组):包括分组危险权限+没有分组的权限adb -s emulator-5554 shell pm list permissions :列出设备emulator-5554上所有的权限adb -s emulator-5554 shell pm list permissions -d -g -f :(-f:表示显示权限的详情)-s emulator-5554 :表示指定设备名称:emulator-5554(可以通过 adb devices来得到设备的列表)
adb -s emulator-5554 shell pm grant permissions.dispatcher.sample android.permission.CAMERA : 许可权限
上面: