"Android Advanced Light" runtime permissions Permission

Android6.0 new features, runtime permissions. To call for example.

step:

1, check the permissions: There are on the phone; not to request permission (if the user previously rejected, first suggesting why need this permission).

2, after requesting permission to come back: if successful on the phone; no success tips is rejected (If you check the "Do not ask me," How is prompted to manually open permissions).

code show as below:


    private void call() {
        
        //检查权限
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            //没有就去申请
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CALL_PHONE)) {
                // 用户拒绝过这个权限了,应该提示用户,为什么需要这个权限。
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setMessage("解释一下,因为你要打电话,所以需要打电话权限。")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                //继续请求权限
                                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PERMISSIOINS_REQUEST_CODE_CALL);
                            }
                        }).create();
                dialog.show();
            }else {
                //请求权限,fragment中运行时权限的特殊处理、在Fragment中申请权限、不要使用ActivityCompat.requestPermissions、直接使用Fragment的requestPermissions方法、否则会回调到Activity的 onRequestPermissionsResult
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PERMISSIOINS_REQUEST_CODE_CALL);
            }
        } else {
            callPhone();
        }

    }

    /**
     * 申请权限的回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSIOINS_REQUEST_CODE_CALL) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone();
            }
            //拒绝后,再次弹框提示申请权限 ,若勾选“不再询问”后点击拒绝,会走此逻辑(此时shouldShowRequestPermissionRationale返回false)。
            //因为已经拒绝且不在提示,需要告诉用户如何打开权限。
            else if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CALL_PHONE)) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setMessage("该功能需要访问电话的权限,当前未打开权限! 打开权限的方法:设置-应用—权限。")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create();
                dialog.show();
            } else {
                Toast.makeText(getContext(), "被拒绝了", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 打电话.
     * <p>
     * 注意:
     * android.intent.action.CALL需要在配置文件中添加拨号权限 且 点击后直接拨号
     * android.intent.action.DIAL只是调用拨号键盘,不用在文件中添加权限
     */
    private void callPhone() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + "10086"));
        startActivity(intent);
    }

 

Renderings

1, the first application for permission, the user refused, prompting the user to why this need to apply for permission again, so that users check the "Do not ask" and refused.

2. Click OK, bomb box will apply for permission "not asking" the check box

3, check the "Do not ask" prompt refused, telling the user how to open permissions.

 

Good article and framework:

Rights Management Best Practices running Android 6.0

At present request permission framework PermissionsDispatcher most popular runtime, RxPermissions and easypermissions use and compare

Published 53 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/hfy8971613/article/details/87183603