目录
adb shell pm list permissions -g
adb shell pm list permissions -s
adb shell dumpsys package permissions
adb shell dumpsys package xxx.xxx.xxx
检查并申请权限 checkAndRequestPermission
权限操作回调 onRequestPermissionsResult
调试命令
adb shell dumpsys package permissions
adb shell dumpsys package <package name>
adb shell pm list permissions
adb shell pm list permissions -s
pm命令
$ adb shell pm
...
list permission-groups
Prints all known permission groups.
list permissions [-g] [-f] [-d] [-u] [GROUP]
Prints all known permissions; optionally only those in GROUP. Options are:
-g: organize by group
-f: print all information
-s: short summary
-d: only list dangerous permissions
-u: list only the permissions users will see
...
adb shell pm list permissions
adb shell pm list permissions -g
adb shell pm list permissions -s
adb shell dumpsys package permissions
adb shell dumpsys package xxx.xxx.xxx
权限授予状态保存在下面文件:
/data/system/users/0/runtime-permissions.xml
调试技巧: 查看有哪些文件被修改了
1. 在设置/应用管理中强行停止app
2. 清除存储
3. touch /data/f1
4. 启动应用并授权
5. find /data -newer /data/f1 : 查看有哪些文件被修改了
检查并申请权限 checkAndRequestPermission
import android.Manifest;
import android.os.Build;
import android.content.pm.PackageManager;
private static String[] PERMISSIONS = {
Manifest.permission.READ_CALENDAR,
"com.android.email.permission.READ_ATTACHMENT",
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.WRITE_CALENDAR,
Manifest.permission.GET_ACCOUNTS,
Manifest.permission.INTERNET,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS};
private static boolean needRequestPermission = false;
private static boolean isAllPermissionsGranted = false;
private static int REQUEST_PERMISSION_CODE = 111;
private void checkAndRequestPermission() {
// check and request permissions
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
for (int i=0;i<PERMISSIONS.length; i++) {
if (checkSelfPermission(PERMISSIONS[i]) != PackageManager.PERMISSION_GRANTED) {
needRequestPermission = true;
}
}
if (needRequestPermission) {
requestPermissions(PERMISSIONS, REQUEST_PERMISSION_CODE);
}
}
}
权限操作回调 onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
LogUtils.d(TAG, "onRequestPermissionsResult: requestCode=" + requestCode + ", permissions=" + Arrays.asList(permissions) +
", grantResults= " + Arrays.toString(grantResults));
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
isAllPermissionsGranted = true;
for (int i=0;i<grantResults.length;i++) {
if (grantResults[i] == -1) {
isAllPermissionsGranted = false;
}
}
LogUtils.d(TAG, "onRequestPermissionsResult: isAllPermissionsGranted=" + isAllPermissionsGranted);
}