Created
October 27, 2017 07:03
-
-
Save amanjain08/4b43e643449df855ff20e325d26f79bb to your computer and use it in GitHub Desktop.
Location Permission and Setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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