【问题标题】:final static boolean variable declare in method在方法中声明最终的静态布尔变量
【发布时间】:2019-09-05 07:46:56
【问题描述】:

我有一个相机权限按钮,在按钮点击监听器上我有 2 个主要的以下条件。 1:如果授予权限,只需转到相机活动... 2:else检查2个进一步的条件(1:我检查(使用布尔变量)如果在对话框中选中““不再要求许可”,然后转到设置....2:else请求许可)。

在 onRequestPermissionsResult 方法中,我检查了用户是否授予权限然后转到相机活动...否则检查用户是否检查了“不再要求权限””设置为 true 布尔变量,否则显示权限被拒绝。

一切正常,但是当我重新启动活动时,它将该布尔变量设置为 false(现在,如果用户可能上次检查了“”不再要求许可“”)并且它应该将用户带到设置但是它不会在第一次单击后单击(当该布尔变量设置为 true 时)它会起作用。 所以我的问题是我如何在 onRequestPermissionsResult 方法中设置该布尔变量 final(因此它不会在活动重启时更改)并在 onCreate 方法中使用它(超出范围)。

这是 onCreate 中的按钮 onclick 监听器代码。

                if (checkPermission())
               {

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 7);
               // Toast.makeText(GalleryImageSelection.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();

            }
            else {
                if (test){ //test is that boolean variable
                    Uri packageUri = Uri.fromParts( "package", getApplicationContext().getPackageName(), null );

                    Intent intent = new Intent();

                    intent.setAction( Settings.ACTION_APPLICATION_DETAILS_SETTINGS );
                    intent.setData( packageUri );
                    intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );

                    startActivity( intent );
                }else {
                ActivityCompat.requestPermissions(GalleryImageSelection.this,new String[]{
                        Manifest.permission.CAMERA}, RequestPermissionCode);

            } }

这里是 onRequestPermissionsResult 方法代码

@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

    switch (RC) {

        case RequestPermissionCode:

                if (PResult[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(GalleryImageSelection.this, "Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, 7);

                } else {
                   // for (int i = 0, len = per.length; i < len; i++) {
                     //   String permission = per[i];
                        if (PResult[0] == PackageManager.PERMISSION_DENIED){
                    boolean showRational =shouldShowRequestPermissionRationale(per[0]);
                    if (!showRational){
                        test = true;//test is declared above onCreate for 
                                    //scope purpose
                    }
                    else if (CAMERA.equals(per[0])) {
                        Toast.makeText(GalleryImageSelection.this, "Permission Denied, your application cannot access CAMERA.", Toast.LENGTH_LONG).show();

                    }
                }
                    }

                break;
            }
    }

【问题讨论】:

  • 为什么不使用 sharedpreference 来跟踪。

标签: android variables scope sharedpreferences


【解决方案1】:

您可以在 onRequestPermissionsResult 上使用ActivityCompat#shouldShowRequestPermissionRational

原答案:Link

【讨论】:

    【解决方案2】:

    无需使用该布尔值来处理权限

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (checkSelfPermission(Manifest.permission.CAMERA)  != PackageManager.PERMISSION_GRANTED) {
                            requestPermissions(new String[]{Manifest.permission.CAMERA}, 1001);
                        } else {
                           Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 7);
                        }
                    } else {
                        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 7);
                    } 
    

    在 onRequestPermissionsResult 中

     @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (grantResults != null && grantResults.length > 0) {
                switch (requestCode) {
                    case 1001:
                        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                             Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 7);
                        } else {
                             if (ActivityCompat.shouldShowRequestPermissionRationale(HomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            Toast.makeText(this, "Please allow permission", Toast.LENGTH_SHORT).show();
                        } else {
                            AlertDialog dialog= new AlertDialog.Builder(HomeActivity.this)
                                    .setMessage("Apply permission")
                                    .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
    
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            Uri packageUri = Uri.fromParts( "package", getApplicationContext().getPackageName(), null );
                                            Intent intent = new Intent();
                                            intent.setAction( Settings.ACTION_APPLICATION_DETAILS_SETTINGS );
                                            intent.setData( packageUri );
                                            intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                                            startActivity( intent );
                                        }
    
                                    })
                                    .create();
                            dialog.show();
                        }
                        }
                        break;
    
                }
            }
        }
    

    【讨论】:

    • 没有帮助,我正在处理“当应用程序显示请求权限对话框时,如果用户单击不再显示复选框并拒绝权限,下次用户再次单击按钮时,它应该将用户带到设置。它的工作但问题是当用户重新启动活动时,它不会第一次工作,
    【解决方案3】:

    我通过使用 sharedpreference 解决了这个问题。我将布尔值放入 sharedpreference 中,如下所示:

    if (!showRational){
                            boolean isChecked= true;
                            sharedpreferences = getSharedPreferences(mypreference,
                                    Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedpreferences.edit();
                            editor.putBoolean("isChecked", isChecked);
                            editor.commit();
                        }
    

    然后在按钮上点击如下:

     sharedpreferences = getSharedPreferences(mypreference,
                        Context.MODE_PRIVATE);
                boolean checked = sharedpreferences.getBoolean("isChecked", false);
                if (checkPermission())
                {
                    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, 7);
                }
                else if(checked)
                {
                    openSetting();
                }else {
    
                    requestPermission();
                }
    

    现在当我重新启动活动时,它不会改变布尔变量的值。

    所以结论是“如果有人想使用变量:

    - 应该在一个方法中声明和初始化,并且应该在另一个方法中调用(超出范围), -(OR) 需要在 METHOD 中初始化一次然后永远不应该更改,Plus 应该在 Any Method 中可用。

    我们将该变量的值保存在 sharedpreference 中,并在我们想要的任何地方获取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-19
      • 2012-11-14
      • 1970-01-01
      • 2011-03-23
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多