android 原生开发 动态获取权限问题

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions, int[] grantResults) {
    System.out.println("77"+requestCode);
    System.out.println(grantResults);
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
            System.out.println("88"+PackageManager.PERMISSION_GRANTED);
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // do your stuff
                System.out.println("99999");
            } else {
                Toast.makeText(IndexActivity.this, "GET_ACCOUNTS Denied",
                        Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            System.out.println("101010");
            super.onRequestPermissionsResult(requestCode, permissions,
                    grantResults);
    }
}
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;

public boolean checkPermissionREAD_EXTERNAL_STORAGE(
        final Context context) {
    System.out.println("now----------------");
    int currentAPIVersion = Build.VERSION.SDK_INT;
    System.out.println("now++++"+currentAPIVersion);
    System.out.println("now+"+android.os.Build.VERSION_CODES.M);
    if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
        System.out.println("no000000*****0"+Manifest.permission.READ_EXTERNAL_STORAGE);
        System.out.println("no000000*****1"+ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE));
        System.out.println("no00000022222222"+PackageManager.PERMISSION_GRANTED);
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            System.out.println("111111111111");
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    (Activity) context,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                System.out.println("5555555");
                showDialog("External storage", context,
                        Manifest.permission.READ_EXTERNAL_STORAGE);

            } else {
                System.out.println("666666666666");
                ActivityCompat
                        .requestPermissions(
                                (Activity) context,
                                new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                                MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
            return false;
        } else {
            return true;
        }

    } else {
        return true;
    }
}
public void showDialog(final String msg, final Context context,
                       final String permission) {
    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
    alertBuilder.setCancelable(true);
    alertBuilder.setTitle("Permission necessary");
    alertBuilder.setMessage(msg + " permission is necessary");
    alertBuilder.setPositiveButton(android.R.string.yes,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions((Activity) context,
                            new String[] { permission },
                            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                }
            });
    AlertDialog alert = alertBuilder.create();
    alert.show();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值