最佳做法是,如果用户拒绝授予您作为开发者的权限,那么您应该以这样一种方式设计您的程序,以便用户仍然能够使用您的应用。因此,如果用户拒绝您的请求,您可以降级您的应用程序。但是,如果您下定决心不能这样做,请执行以下操作。
请求权限是否仅在用户事件期间发生,例如当用户单击按钮时,在编辑文本等中写入一些文本。
如果是这样,为什么不在视图中添加点击实现(例如按钮、文本视图),以便每次点击它时检查用户是否已授予权限,如果没有,则再次请求权限。
您的代码应如下所示。
注意:这强烈基于@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、用户未允许、拒绝或通过选择拒绝和不再询问时拒绝该权限时显示。如果用户选择了拒绝并且不再询问,则只有他们可以在应用程序设置中更改它,开发人员可以做的就是使用意图将他们引导至设置。
同样根据 android 文档“从 Android 11(API 级别 30)开始,如果用户在您的应用程序在设备上安装的生命周期内多次点击拒绝特定权限,则用户不会看到系统权限对话框,如果您的应用再次请求该权限。用户的操作意味着“不要再询问。”因此,如果您在使用 android 11 的设备上进行测试,这将会发生,您可能必须将用户引导至本例中的应用程序也是如此。
如果用户拒绝,您也可以关闭您的应用,但我不建议这样做。
有关请求权限的更多信息,请访问 android 文档。
这里:https://developer.android.com/training/permissions/requesting#handle-denial