I want to request for user's location every time the activity comes into screen (I think my only option here is activity's onResume()
method) and I want to (and should) support android 6's new runtime permission model.
I am using permissionDispatcher library and it works perfectly. The problem is as this issue suggests , if I call a function which requests a permission in the onResume()
method and user denies the permission, it will stuck in an infinite loop.
But my problem is that since I want to get the user's location on every appearance of the activity, I cannot think of a different way other than using the onResume()
method (and hence, getting stuck in an infinite loop if the user denies the permission).
here's a sample of my code:
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
void startLocationUpdate() {
mLocationLoadingDialog.show();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@OnShowRationale(Manifest.permission.ACCESS_FINE_LOCATION)
void showRationalForFineLocation(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage("this app needs your permission to get your location")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.proceed();
}
})
.setNegativeButton("no way", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.cancel();
exitApp();
}
})
.setCancelable(false)
.show();
}
@OnPermissionDenied(Manifest.permission.ACCESS_FINE_LOCATION)
void showDeniedForFINELOCATION() {
exitApp();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
RequestSelectorActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
private void exitApp() {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("you didn't allow us to know where you are, so the application will exit")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
}).show();
}
what can I do for my situation?
Until unless the permission is so necessary that we have to close the activity without the permission, I don't recommend requesting permissions in onResume().
If you deny a permission without never ask again flag set true, as many number of times onResume() is called that many times you have to request for permissions.
I do understand that if you consider all the possible cases you might want to put it in onResume(), but think that it may also annoy user in some use cases.
I suggest you to put requesting part in onStart() instead of onResume()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.