Created
December 5, 2011 18:54
-
-
Save jimmy-collazos/1434764 to your computer and use it in GitHub Desktop.
Snippet/Android: Toggle GPS
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
//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