onrequestpermissionsresult?

ujv3wf0j  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(456)

在我的应用程序中,我在oncreate()函数中请求如下权限

if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED)
            registerListener(context);
        else if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.ACTIVITY_RECOGNITION))
            new AlertDialog.Builder(context)
                    .setTitle("Permission required")
                    .setMessage("This permission is required to fetch the sensor data from your phone, denying it will cause the app to exit")
                    .setPositiveButton("Give permission", (dialog, which) -> ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_REQUEST_CODE))
                    .setNegativeButton("Deny", (dialog, which) -> ((Activity) context).finish())
                    .show();
        else
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_REQUEST_CODE);

这是onrequestpermissionsresult重写的方法

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == presenter.ACTIVITY_REQUEST_CODE)
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                presenter.registerListener(this);
            else {
                Toast.makeText(this, "Permission denied, exiting", Toast.LENGTH_LONG).show();
                finish();
            }
    }

现在我面临的问题是,我得到了适当的弹出窗口请求许可,但我看到了祝酒词的消息( Permission denied, exiting )在我做出选择之前 finish() 方法在我做出选择之前调用应用程序退出!
我不知道为什么会这样,任何帮助都是非常感谢的!

oalqel3c

oalqel3c1#

事实上 onRequestPermissionsResult 被称为完美的预期。你必须在它体内处理这个案子。你的问题是 Activity 完成之前 dialog 打开来做任何选择。所以,试着评论一下 finish() 方法内部 onRequestPermissionsResult . 请看下面的代码以获得更好的清晰度。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == presenter.ACTIVITY_REQUEST_CODE)
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            presenter.registerListener(this);
        else {
            // if user denies permission then Dialog would be pop up. That's why I commented out both toast and finish() method below.
            // If user clicks deny on the dialog then only finish activity.
            //Toast.makeText(this, "Permission denied, exiting", Toast.LENGTH_LONG).show();
            //finish();
        }
}

在代码的以下部分处理用户拒绝情况:

.setNegativeButton("Deny", (dialog, which) -> {
                        // Put code here for user denial------
                        finish();
                    })
guz6ccqo

guz6ccqo2#

解决方案就在这里

使用dexter让你的生活更轻松
(1) 在build.grade(应用程序级别)文件中添加以下依赖项

implementation ‘com.karumi:dexter:6.2.2’

(2) 现在就用它。。

Dexter.withActivity(this)
                // below line is use to request the number of 
                // permissions which are required in our app.
                .withPermissions(Manifest.permission.CAMERA,
                        // below is the list of permissions
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_CONTACTS)
                // after adding permissions we are 
                // calling an with listener method.
                .withListener(new MultiplePermissionsListener() {
                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
                        // this method is called when all permissions are granted
                        if (multiplePermissionsReport.areAllPermissionsGranted()) {
                            // do you work now
                            Toast.makeText(MainActivity.this, "All the permissions are granted..", Toast.LENGTH_SHORT).show();
                        }
                        // check for permanent denial of any permission
                        if (multiplePermissionsReport.isAnyPermissionPermanentlyDenied()) {
                            // permission is denied permanently,
                            // we will show user a dialog message.
                            showSettingsDialog();
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
                        // this method is called when user grants some 
                        // permission and denies some of them.
                        permissionToken.continuePermissionRequest();
                    }
                }).withErrorListener(new PermissionRequestErrorListener() {
            // this method is use to handle error 
            // in runtime permissions
            @Override
            public void onError(DexterError error) {
                // we are displaying a toast message for error message.
                Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
            }
        })
                // below line is use to run the permissions
                // on same thread and to check the permissions
                .onSameThread().check();`
qkf9rpyu

qkf9rpyu3#

我建议,与其创建一个对话框,不如让android来弥补。检查是否授予权限

private boolean checkRECOGNITIONPermission(){
        final boolean isReturn = ContextCompat.checkSelfPermission
                     (this, Manifest.permission.Manifest.permission.ACTIVITY_RECOGNITION)
                           == PackageManager.PERMISSION_GRANTED;
        return isReturn;
    }

然后请求许可

private void requestStoragePermissionExport(){     
      ActivityCompat.requestPermissions
     (this,new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_REQUEST_CODE);
 }

检查是否授予权限

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // handle permission request
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case ACTIVITY_REQUEST_CODE : 
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    // permission granted do your work

                }else {
                    Toast.makeText(this, Permission Required, Toast.LENGTH_SHORT).show();
                }
                break;          
            }
        }

当你想查看权限的时候就叫这个

if(checkRECOGNITIONPermission()){
   // Do work (Permission granted)
}else{
   checkRECOGNITIONPermission(); // request permission
   if(!checkRECOGNITIONPermission()){
       finsih();
   }
}

相关问题