Skip to content

Instantly share code, notes, and snippets.

@amanjain08
Created October 27, 2017 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amanjain08/4b43e643449df855ff20e325d26f79bb to your computer and use it in GitHub Desktop.
Save amanjain08/4b43e643449df855ff20e325d26f79bb to your computer and use it in GitHub Desktop.
Location Permission and Setting
/**
* Call this method to check Location Settings before proceeding for UserLogin
*/
private void checkForLocationSettings() {
// Check for Location permission
if (!HyperTrack.checkLocationPermission(this)) {
HyperTrack.requestPermissions(this);
return;
}
// Check for Location settings
if (!HyperTrack.checkLocationServices(this)) {
HyperTrack.requestLocationServices(this);
}
// Location Permissions and Settings have been enabled
// Proceed with your app logic here i.e User Login in this case
attemptUserLogin();
}
/**
* Handle on Grant Location Permissions request accepted/denied result
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == HyperTrack.REQUEST_CODE_LOCATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
// Check if Location Settings are enabled to proceed
checkForLocationSettings();
} else {
// Handle Location Permission denied error
Toast.makeText(this, "Location Permission denied.",
Toast.LENGTH_SHORT).show();
}
}
}
/**
* Handle on Enable Location Services request accepted/denied result
*
* @param requestCode
* @param resultCode
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == HyperTrack.REQUEST_CODE_LOCATION_SERVICES) {
if (resultCode == Activity.RESULT_OK) {
// Check if Location Settings are enabled to proceed
checkForLocationSettings();
} else {
// Handle Enable Location Services request denied error
Toast.makeText(this, R.string.enable_location_settings,
Toast.LENGTH_SHORT).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment