Android M 最简单的权限请求

private void requestPermission() {

    //1. 检查是否已经有该权限

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SET_ALARM)

    != PackageManager.PERMISSION_GRANTED) {

    //2. 权限没有开启,请求权限

    ActivityCompat.requestPermissions(this,

    new String[]{Manifest.permission.SET_ALARM}, Location_Permission);

    }else{

    isAgreePermission=true;

    //权限已经开启,做相应事情

    }

    }

    //3. 接收申请成功或者失败回调

    @Override

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == Location_Permission) {

    if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

    //权限被用户同意,做相应的事情

    isAgreePermission=true;

    } else {

    //权限被用户拒绝,做相应的事情

    isAgreePermission=false;

    }

    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

你可能感兴趣的:(Android M 最简单的权限请求)