Skip to content

Instantly share code, notes, and snippets.

@jimmy-collazos
Created December 5, 2011 18:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimmy-collazos/1434764 to your computer and use it in GitHub Desktop.
Save jimmy-collazos/1434764 to your computer and use it in GitHub Desktop.
Snippet/Android: Toggle GPS
//recordar activar permisos
//<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
private void toggleGPS(boolean enable) {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps") == enable) {
return; // the GPS is already in the requested state
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Esta aplicación necesita activar el GPS. ¿Desea activar el GPS?")
.setCancelable(false)
.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Toggle GPS
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
getApplicationContext().sendBroadcast(poke);
//end
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
//TrackActivity.this.finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment