I am trying to enable/disable (toggle) GPS programmatically in my app via a button click, but it is not working. Please help.
My target devices are not rooted.
Here’s the code I’m using.
private void gps()
{
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
Button gps = (Button)findViewById(R.id.gpsButton);
if(isGPSon())
{
intent.putExtra("enabled", true);
gps.setText(R.string.f2_gps_deact);
}
else
{
intent.putExtra("enabled", false);
gps.setText(R.string.f2_gps_act);
}
sendBroadcast(intent);
You can’t enable GPS programatically. The most you can do is open the settings for the user to do that.
You can do it like this: