Skip to content

Instantly share code, notes, and snippets.

@piyushgupta27
Created March 6, 2017 13:38
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 piyushgupta27/9ac95da91242e73073120e18a58bc2fc to your computer and use it in GitHub Desktop.
Save piyushgupta27/9ac95da91242e73073120e18a58bc2fc to your computer and use it in GitHub Desktop.
Implementation for requesting Location Permissions and Location Services using HyperTrack APIs
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Check if location settings have been enabled by the user
checkForLocationSettings();
}
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, null);
}
// Location Permissions and Settings have been enabled
// Proceed with your app logic here
}
@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) {
checkForLocationSettings();
} else {
// Handle Location Permission denied error
Toast.makeText(this, "Location Permission denied.", Toast.LENGTH_SHORT).show();
}
}
}
@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) {
checkForLocationSettings();
} else {
// Handle Enable Location Services request denied error
Toast.makeText(this, "Enable Location Services request denied.", Toast.LENGTH_SHORT).show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment