Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Last active July 12, 2016 06:59
Show Gist options
  • Save talhahasanzia/9e493d19b54a18fe4c9652077ad945ee to your computer and use it in GitHub Desktop.
Save talhahasanzia/9e493d19b54a18fe4c9652077ad945ee to your computer and use it in GitHub Desktop.
Location permission handling in Marshmallow and above. (Permissions at runtime)
void enableGPS() {
// locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
else
checkIfGPSIsOn(locMan, this);
}
else
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, this);
}
//----------------------------------------------------------------------------------------------------------------------
@SuppressWarnings({"MissingPermission"}) @Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, this);
} else {
// permission denied
Toast.makeText(this, "GPS Disabled, enable GPS and allow app to use the service. Or app will not work properly", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
//----------------------------------------------------------------------------------------------------------------------
protected void checkIfGPSIsOn(LocationManager manager, Context context) {
if (!manager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Location Manager");
builder.setMessage("Masjid Locator requires a device with GPS to work");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
} else {
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Ask the user to enable GPS
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Location Manager");
builder.setMessage("Masjid Locator would like to enable your device's GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Launch settings, allowing user to make a change
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//No location service, no Activity
finish();
}
});
builder.create().show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment