问题描述
我想提醒用户授予位置权限,因为位置是应用程序的关键功能.
I want to nag user to give the location permission, since location is a key piece of functionality for the app.
我尝试检查 onResume
的权限,但效果不佳,用户可以在应用程序之间来回切换,并且已经可以显示权限对话框.我正在考虑使用一个标志来知道显示了权限对话框,所以是这样的:
I tried with checking on onResume
for permission, but it doesn't give good results, user can switch back and forth between the app and the permission dialog can already be displayed.I am thinking to use a flag to know that permission dialog is displayed, so something like this:
public void onResume(...)
{
if(!isDialogDisplayed)
{
isDialogDisplayed = true;
// check for permission here
}
}
我需要在关闭对话框时将 isDialogDisplayed
设置为 false
.
I need to set isDialogDisplayed
to false
when dialog is dismissed.
我怀疑这是最好的方法,还有其他方法吗?
I have doubts this is the best way to do this, any other ways to do this?
推荐答案
在你的 MainActivity 的 onStart 方法中,运行这个方法:
In your MainActivity's onStart method, run this method:
private void checkForPermissions() {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
} else {
initializeViews();
}
}
initializeViews() 是让您的应用程序正常启动.
initializeViews() is to make your app start up like normal.
然后实现 onRequestPermissionsResult() 如下:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_FINE_LOCATION) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initializeFragments();
} else {
openAlertDialog();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
MY_PERMISSIONS_REQUEST_FINE_LOCATION 是最终的 int 字段,在我的例子中是 100.该值无关紧要,只要您在两种方法中使用相同的变量即可.
MY_PERMISSIONS_REQUEST_FINE_LOCATION is a final int field, which in my case is 100. The value does not matter, as long as you use the same variable in both methods.
如果用户不允许权限,openAlertDialog() 将被调用,在我的应用程序中如下:
If the user did not allow the permission, openAlertDialog() will be called, which in my app is the following:
private void openAlertDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("This app requires your location to function!");
alertDialogBuilder.setPositiveButton("Try again",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
checkForPermissions();
}
});
alertDialogBuilder.setNegativeButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:dk.redweb.intern.findetlokum"));
startActivity(i);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
此方法创建一个对话窗口,用户可以在其中选择再次获取权限请求,或打开应用的设置"菜单.当用户从设置"返回应用时,将再次调用 onStart 以检查是否已授予权限.
This method makes a dialog window, where the user will be given a choice between getting the permission request again, or opening up the Settings menu for your app. When the user returns to the app from Settings, onStart will be called again to check if the permission has been granted.
这篇关于如何唠叨\询问用户启用权限,直到用户给予它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!