【发布时间】: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