Android6.0的动态权限的简单使用

权限工具activity:

public class BaseActivity extends AppCompatActivity {
    public String[] permissions = new String[]{ //申请的权限
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.BODY_SENSORS,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 判断系统版本是否是Android6.0以上
     * @return
     */
    protected boolean isSystemVersionLarge23(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 检查多个权限是否被授予
     * @param permissions 被检查权限名
     * @return
     */
    protected boolean checkMorePermission(String[] permissions){
        boolean isAllCheck = false;
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this,permissions[i]) != PackageManager.PERMISSION_GRANTED){
                isAllCheck = true;
                break;
            }
        }
        if (isAllCheck){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 检查单个权限是否被授予
     * @param permission 被检查权限名
     * @return
     */
    protected boolean checkOnePermission(String permission){
        if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED){
            return false;
        }else{
            return true;
        }
    }


    /**
     * 请求权限一个或多个
     * @param permissions
     * @param requestCode
     */
    protected void requestOneOrMorePermissions(String[] permissions,int requestCode,AppCompatActivity activity){
        boolean isAllCheck = false;
        for (int i = 0; i < permissions.length; i++) {
            if (!checkOnePermission(permissions[i])){
                isAllCheck = true;
                break;
            }
        }
        if (isAllCheck){
            ActivityCompat.requestPermissions(activity,permissions,requestCode);
        }
    }


    /**
     * 获取应用版本号
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    protected float getVersionCode(){
        float versionCode = 0;
        try {
            versionCode = getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public void requestSDcardPermission(AppCompatActivity activity,int resultcode){
        //检查SDcard权限
        if (activity.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                && activity.checkCallingOrSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},resultcode);
        }


    }
    public void requestSensorPermission(AppCompatActivity activity,int resultcode){
        //检查传感器权限
        if (activity.checkCallingOrSelfPermission(Manifest.permission.BODY_SENSORS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.BODY_SENSORS},resultcode);
        }
    }

    public void requestPositionPermission(AppCompatActivity activity,int resultcode){
        //检查位置权限
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},resultcode);
        }
    }
}

继承Baseactivity一次请求多个权限:

public class LauchActivity extends BaseActivity {
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lauch_activity);

        //判断系统版本是否是6.0
        if (isSystemVersionLarge23()){
            checkPermission();
        }else {
            prepare();
        }

    }

    private void prepare(){
 		//操作
    }


    private void checkPermission(){

        if (checkMorePermission(permissions)){
                prepare();
        }else {
            requestOneOrMorePermissions(permissions,11,this);//一次请求多个权限
        }
    }


    //申请权限回调  Android6.0以上才会调用 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 11:
                if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //所有权限通过才会
                    prepare();
                }else {
                    //拒绝一个就会这出现
                    showHintDialog("获取权限失败!");
                }
                break;
        }
    }

    /**
     * 拒绝权限弹出提示
     * @param messege
     */
    protected void showHintDialog(String messege){
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage(messege);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checkPermission();
                dialog.dismiss();
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

继承Baseactivity一次请求一个权限:

 
  
requestSDcardPermission(this,111);
requestSensorPermission(this,112);
requestPositionPermission(this,113);
/**
 * Android6.0以上动态权限回调
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 111:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //权限通过
            }else {
                //拒绝一个就会这出现
                
            }
            break;
        case 112:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //权限通过

            }else {
                //拒绝一个就会这出现
                
            }
            break;
        case 113:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //所有权限通过才会
            }else {
                //拒绝一个就会这出现
                
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode,permissions,grantResults);
            break;
    }

}


你可能感兴趣的:(Android)