Is GPS on? If not prompt user to turn it on.
Here’s a quick code snippet to determine if the GPS is on, and if it isn’t go to the Setting screen allowing the user to turn it on. Android does not allow a 3rd party app to automatically turn the GPS on/off as of 1.5, a “feature” discussed extensively on the Android Developers Forum.
private void openGPSSettings()
{
LocationManager alm =
(LocationManager)this.getSystemService( Context.LOCATION_SERVICE );
if( alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
{
Toast.makeText( this, "GPS is already on", Toast.LENGTH_SHORT ).show();
}
else
{
Toast.makeText( this, "Please turn on GPS", Toast.LENGTH_SHORT ).show();
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
startActivity(myIntent);
}
}
Don’t forget to add the following permission to the AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

November 3rd, 2009 at 5:30 pm
[…] (more…) All, Programming […]
July 22nd, 2010 at 8:01 pm
Hi your code snippet is helpful!!!! Thanks very much