Android app权限调试,checkAndRequestPermission

1 篇文章 0 订阅

目录

调试命令

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

权限授予状态保存在下面文件:

调试技巧:

检查并申请权限 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);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值