对 SYSTEM_ALERT_WINDOW 和 WRITE_SETTINGS 的处理

最近开始开始研究 Android 6.0 的适配,关于两个特殊权限,查阅了一下资料,对于处理方法,在这里做个记录。

SYSTEM_ALERT_WINDOW

@TargetApi(Build.VERSION_CODES.M)
    private void requestDrawOverLays() {
        if (!Settings.canDrawOverlays(SettingActivity.this)) {

            AlertDialog dialog = new AlertDialog.Builder(SettingActivity.this)
                    .setTitle("需要您手动授予权限")
                    .setMessage("点击 OK 进入设置页面手动开启权限,允许本应用在其他应用界面之上打开")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        //核心代码
                            Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                    Uri.parse("package:" + getPackageName()));
                            startActivityForResult(i, OVERLAY_PERMISSION_REQ_CODE);
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mSwSms.setChecked(false);
                        }
                    })
                    .create();
            dialog.show();
        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
            if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "Permission Denied by user.", Toast.LENGTH_SHORT)
                        .show();
                mSwSms.setChecked(false);
            } else {
                Toast.makeText(this, "Permission allowed", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == QRCODE_RESULT) {
            if (resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                String scanResult = bundle.getString("result");
                mScanResult.setText(scanResult);
            }
        }
    }

WRITE_SETTINGS

@TargetApi(23)
    private void writeSettingsPermission() {
        if(!Settings.System.canWrite(this)){
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("WRITE_SETTINGS权限申请")
                    .setMessage("点击OK进入设置界面授予权限")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //核心代码
                            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                                    Uri.parse("package:" + getPackageName()));
                            startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS);
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .create();
            dialog.show();
        }
    }

    @Override
    @TargetApi(23)
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
            if (Settings.System.canWrite(this)) {
                Toast.makeText(this, "WRITE_SETTINGS permission granted", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(this, "WRITE_SETINGS permission denied", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

你可能感兴趣的:(Android)