用户更改已从“设置”屏幕

时间:2017-08-28 09:56:22

标签: android permissions android-permissions

应用程序需要两个权限

  1. 阅读手机状态
  2. 摄像机
  3. 应用程序有以下屏幕:

    屏幕A:启动(根据权限状态处理屏幕导航)

    屏幕B:权限屏幕(如果用户未授予电话权限)

    屏幕C:条款&条件屏幕

    当它启动时,App将检查是否授予了电话许可。如果被授予,app将打开屏幕C,否则它将保留在同一屏幕B上。

    步骤:

    1. 打开应用程序 - >飞溅 - >权限屏幕
    2. 点击'授予权限' - >权限对话框 - >拒绝许可
    3. 点击'打开设置'
    4. 格兰特'相机'许可(不是电话许可)
    5. 点击'返回'设备按钮并返回应用程序。
    6. 权限屏幕将显示为未授予'电话' 权限。
    7. 再次重复步骤#2和#3
    8. 现在移除相机'许可和授权'电话'权限(因为我们已经更改了相机权限应用程序进程将被终止并重新开始)。
    9. 重复步骤#5,Splash,然后将显示屏幕C(条款和条件)。
    10. 再次点击'返回'按钮,将显示相同的屏幕C(屏幕C在后栈中有两个实例)
    11. 我可以使用' CLEAR_TASK' 和' CLEAR_TOP' 标志和startActivity,目的是了解我们的应用程序会发生什么当用户更改时,设置权限。

      观察:

      1. 当用户授予权限时,我们的申请流程将无效。

      2. 当用户删除授予的权限时,Android应用会终止并重启应用。 (如果发生这种情况,那么为什么有两个T& C屏幕实例?)

      3. 我在这里有点困惑!!

2 个答案:

答案 0 :(得分:1)

@AndiGeeky以下是一些完整的代码,用于通知用户是否选择了“允许”或“拒绝”,并且它将管理用户选择“永不再问”或用户在允许不允许的情况下更改设置的其他两个方案

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //.... write file into storage ...
            System.out.println("SDK > BuildVersion TRUE");
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 666);  // Comment 26
            System.out.println("go to requestPermissions");
        }
    }
    onLoad();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {

        case 666: // Allowed was selected so Permission granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Permission Granted",Snackbar.LENGTH_LONG);
                View snackbarView = s.getView();
                TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.RED);
                textView.setTextSize(18);
                textView.setMaxLines(6);
                s.show();

                // do your work here

            } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {
                // User selected the Never Ask Again Option Change settings in app settings manually
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setTitle("Change Permissions in Settings");
                alertDialogBuilder
                        .setMessage("" +
                                "\nClick SETTINGS to Manually Set\n"+"Permissions to use Database Storage")
                        .setCancelable(false)
                        .setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivityForResult(intent, 1000);     // Comment 3.
                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            } else {
                    // User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                    alertDialogBuilder.setTitle("Second Chance");
                    alertDialogBuilder
                            .setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
                            .setCancelable(false)
                            .setPositiveButton("RETRY", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    //ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
                                    Intent i = new Intent(MainActivity.this,MainActivity.class);
                                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(i);
                                    }
                            })
                            .setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    finish();
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }
            }
            break;
    }};

答案 1 :(得分:0)

/**** If user changes, permission from settings it will again ask for permission to 
      granted.You should always check if user changes permission from settings
      using below code ***/ 

if(checkSelfPermission(Manifest.permission.your_permission_name) == PackageManager.PERMISSION_GRANTED)