请求权限 - 通过应用程序设置处理结果

时间:2018-03-26 12:34:20

标签: android visibility android-permissions runtime-permissions

现在我有一个需要使用相机的应用程序。当应用程序被触发时,摄像机需要自动打开Pre-M设备并请求M设备后的权限。现在我有一个textview,button和一个imageview,当授予权限时它会消失。这是代码 的 onCreateMethod:

imageView = findViewById(R.id.qr_image);
        textView = findViewById(R.id.allowPermission);
        allow = findViewById(R.id.allowPermissionButton);
        allow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            checkPermission();
            }
        });
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            imageView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);
            allow.setVisibility(View.VISIBLE);
        }
        else {
            //start camera
        }
    }

这是请求权限方法:

private void checkPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            // Permission is already available, show camera
            //Start camera here
            imageView.setVisibility(View.GONE);
            textView.setVisibility(View.GONE);
            allow.setVisibility(View.GONE);
        }
        else {
            requestPermission();
        }
    }
    private void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            // Request permission
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA);

        }
        else {
            // Request the permission. The result will be received in onRequestPermissionResult().
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA);
        }
    }
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);
                    allow.setVisibility(View.GONE);
                    //permission received start camera here
                }
                else {
                    Snackbar snackbar = Snackbar.make(layout, "Camera and Storage permissions are required.",
                            Snackbar.LENGTH_INDEFINITE);
                    snackbar.setActionTextColor(Color.RED);
                    snackbar.setAction("Enable", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        }
                    }).show();
                            }
                break;
        }
    }

我的onResume方法&我有覆盖的onDestroy:

@Override
    public void onResume() {
        super.onResume();
            //Start camera here
        }
    @Override
    public void onDestroy() {
        super.onDestroy();
        //Stop camera here
    }

现在一切都很完美,如果我通过运行时启用权限,相机就会启动。现在问题开始时,我已经不要再问,我的应用程序会指向设置。现在我去那里并启用相机许可并回来。 Ta-da相机显示在我的屏幕上,同时文本视图,图像视图和按钮挂在屏幕上。如何解决这个问题?

注意:现在,在您建议我隐藏textView,imageview和onResume中的按钮之前,我已尝试过。基本上它总是隐藏,因为即使在我拒绝许可后也会执行onResume。

  

对我来说似乎是一个可能的错误。我知道他们在文档中   如果用户拒绝,建议关闭应用程序。但事实并非如此   一个可能的解决方案。有解决方法吗?

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题

首先不要在onCreate()

中调用此方法
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            imageView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);
            allow.setVisibility(View.VISIBLE);
        }
        else {
            //start camera
        }
    }

onRequestPermissionsResult()应该是这样的

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);
                    allow.setVisibility(View.GONE);
                    //permission received start camera here
                }
                else {
                 // you have not shown this when the permissions are declined by user, need to show here
                imageView.setVisibility(View.VISIBLE);
                textView.setVisibility(View.VISIBLE);
                allow.setVisibility(View.VISIBLE);

                    Snackbar snackbar = Snackbar.make(layout, "Camera and Storage permissions are required.",
                            Snackbar.LENGTH_INDEFINITE);
                    snackbar.setActionTextColor(Color.RED);
                    snackbar.setAction("Enable", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        }
                    }).show();
                            }
                break;
        }
    }

并在onCreate()

中调用它
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            // Permission is already available, show camera

            imageView.setVisibility(View.GONE);
            textView.setVisibility(View.GONE);
            allow.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);
            allow.setVisibility(View.VISIBLE);
        }

    }