如果在android中被拒绝,如何再次请求许可

new*_*ect 18 permissions android

用户如何拒绝,然后对话框就不再弹出,这是我使用的代码:

ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

有人可以给我一些帮助,如何让对话框再次弹出吗?提前致谢。

new*_*ect 14

好吧,事实证明 android 11 只请求两次许可,而不是以前的版本一遍又一遍地请求(这是我从一开始就期待的,因此对于为什么该功能只工作两次感到困惑)。

  • 因此,如果用户拒绝两次(错误地),我们如何注意到并将用户重定向到设置页面?@新项目 (19认同)
  • @SamanSattari 我检测询问和拒绝状态之间的回调时间是否<400ms,并假设其被拒绝而不显示对话框 (2认同)

Dan*_*rds 5

最佳实践是,如果用户拒绝许可,您作为开发人员应该以这样的方式设计您的程序,以便用户仍然能够使用您的应用程序。因此,如果用户拒绝您的请求,您可以降级您的应用程序。但是,如果您决定不能这样做,那么请执行以下操作。

请求权限是否仅在用户事件期间发生,例如当用户单击按钮、在编辑文本中写入一些文本等时。

如果是这样,为什么不向视图(例如按钮、文本视图)添加点击实现,以便每次单击它时,您都会检查用户是否已授予权限,如果没有,则再次请求权限。

您的代码应该如下所示。注意:这很大程度上基于@javdromero 发送给您的链接答案。我只是举了这个例子,因为你说你在构建它时遇到了麻烦。

  public class MainActivity extends AppCompatActivity {


// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private final ActivityResultLauncher<String> requestPermissionLauncher =
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            if (isGranted) {
                // Permission is granted. Continue the action or workflow in your
                // app.

            } else {
                // Explain to the user that the feature is unavailable because the
                // features requires a permission that the user has denied. At the
                // same time, respect the user's decision. Don't link to system
                // settings in an effort to convince the user to change their
                // decision.
            }

        });


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

// The permission will be checked as the app starts.
@Override
protected void onStart() {
    super.onStart();

// The permission will also be checked on button click
public void myMethod(View view) {
    checkPermissionRequest();
}

// Called if shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) is not true 
// or if the yes button is pressed in the alert dialog.
public void makePermissionRequest() {
    requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
}


public void checkPermissionRequest() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        // continue running app

    } else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
        showAlertDialog();
    } else {
        makePermissionRequest();
    }
}

// is called if the permission is not given.
public void showAlertDialog() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("This app needs you to allow this 
     permission in order to function.Will you allow it");
    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    makePermissionRequest();
                }
            });

    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

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

}


 }
Run Code Online (Sandbox Code Playgroud)

仅当 shouldShowRequestPermissionRationale 方法返回 true、用户未允许该权限、拒绝或通过选择“拒绝”拒绝该权限且不再询问时,警报对话框才会显示。如果用户选择“拒绝”并且不再询问,则只有他们可以在应用程序设置中更改它,作为开发人员,您可以做的就是使用意图将他们引导至设置。另外,根据 Android 文档,“从 Android 11(API 级别 30)开始,如果用户在设备上安装应用程序的生命周期内多次点击“拒绝”特定权限,则用户不会看到系统权限对话框,如果您的应用程序再次请求该权限。用户的操作意味着“不要再询问。”因此,如果您在使用 android 11 的设备上进行测试,这就是将会发生的情况,您可能必须引导用户进行设置在这种情况下,该应用程序也是如此。

如果用户拒绝,您也可以关闭您的应用程序,但我不会真正推荐这样做。

有关请求许可的更多信息,请参阅 android 文档。

此处:https: //developer.android.com/training/permissions/requesting#handle-denial

  • “最佳实践是,如果用户拒绝许可,那么作为开发人员,您应该以这样的方式设计您的程序,以便用户仍然能够使用您的应用程序。” 喜欢没有位置的 GPS 应用程序吗?或者是带有蓝牙设备但没有蓝牙权限的医疗应用程序?这并没有什么不好的做法,也没有什么可以“下定决心”的。该应用程序仅在具有所需权限的情况下才能运行。即使您在技术上不一定需要许可,它仍然可能是业务要求(例如,这是您公司的融资方式)。 (7认同)
  • 这就是我想做的,但由于某种原因它只请求两次许可,之后如果仍未获得许可,则不会再次询问,需要在手机设置中进行更改。 (2认同)