接受权限后如何开启活动?

时间:2017-10-18 14:41:03

标签: java android permissions

我尝试做一个简单的功能。用户接受相机权限后,我需要打开一个新活动,但这是怎么做的?

这是一个代码。我检查了许可和请求。如果我开了一个新活动,就在我的其他地方。

 @RequiresApi(api = Build.VERSION_CODES.M)
    @OnClick(R.id.scanButton)
    void scanZxing(View view) {
        if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
            Toast.makeText(context, "We need permissions to acces your CAMERA!", Toast.LENGTH_SHORT).show();
            ((MainActivity) context).checkCameraPermission();


        } else if (view.getId() == R.id.scanButton) {
           Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
           startActivityForResult(intent, 0);
       }
    }

@TargetApi(23)
public void checkCameraPermission(){
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
        return;
    }
    if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_CAMERA:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "We need your permission to open camera",
                        LENGTH_SHORT).show();
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在活动onRequestPermissionsResult方法中检查权限请求结果,如果用户授予了权限,则打开活动

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE_CAMERA &&
            grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        //permisssion was granted
    } else {

        //permisssion was not granted
    }
}

答案 1 :(得分:1)

我想你想打开ScanBarcodeActvity。

你可以通过以下方式完成

public void openScanBarCodeAcitvity() {

Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
               startActivityForResult(intent, 0);
}

除scanZxing()方法

外,在以下地方调用上述方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_CAMERA:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
                 //permission granted, open activity
                 openScanBarCodeAcitvity();
            } else {
                Toast.makeText(this, "We need your permission to open camera",
                        LENGTH_SHORT).show();
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}