I want to nag user to give the location permission, since location is a key piece of functionality for the app.
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
}
}
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?
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() is to make your app start up like normal.
Then implement the onRequestPermissionsResult() as below:
@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 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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With