繁体   English   中英

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

[英]How to ask again for permission if it was denied in android

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

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

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

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

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

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

您的代码应该看起来像这样。 注意:这强烈基于@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();

}


 }

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

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

有关向 android 文档请求许可 go 的更多信息。

这里: https://developer.android.com/training/permissions/requesting#handle-denial

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM