I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly?
-
depends on the version of Cupcake. 1.5 doesnt allow it apparently. – D3vtr0n Jun 26 '09 at 23:07
You can't, starting with Android 1.5. The most you can do is pop open the activity to allow the user to toggle it on/off. Use the action held in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
to craft an Intent to open this activity.
-
3Why is this disabled? Why not allow developers to toggle this? the Power Control widget can, so we should be able to as well. Dont you think? – Adam Lerman Apr 27 '10 at 12:26
-
24It is disabled for privacy reasons. If the user wants GPS off, the user should have GPS off, period. – CommonsWare Apr 27 '10 at 13:13
-
These guys seem to have figured it out. Unfortunately the apk is obfuscated and I could not figure out how it was accomplished. URL:market.android.com/… – Brian Sweeney Jul 20 '11 at 19:39
-
@commonsware but it can actually be done by exploiting a certain loophole up to 2.2. I'm sure you know the details, but for others, see Enable GPS programatically like Tasker – Richard Le Mesurier Jun 20 '12 at 14:40
-
@RichardLeMesurier: Such security loopholes have been fixed, at least for newer versions of Android. – CommonsWare Jun 20 '12 at 15:19
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
startActivity(myIntent);
}
-
8This code didn't compile for me on android 2.2, as .isProviderEnabled is not a static method on LocationManager. Working code for me was as follows (apologies for formatting) LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) { Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); startActivity(myIntent); } – Ben Clayton Jan 28 '11 at 17:26
This method code can be help for you
private void turnGPSOnOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("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"));
sendBroadcast(poke);
//Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
}
}
-
2yes this can be done up to 2.2 (sdk 8). For more info, see Enable GPS programatically like Tasker – Richard Le Mesurier Jun 20 '12 at 14:38
You might use the following:
try {
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
} catch (Exception e) {
logger.log(Log.ERROR, e, e.getMessage());
}
but it will only work if you have system signature protection level. So you need to cook your own Image to actually use it :/
First check whether the location service is already on or not ??
Checking location service is enabled or not
public boolean isLocationServiceEnabled(){
LocationManager locationManager = null;
boolean gps_enabled= false,network_enabled = false;
if(locationManager ==null)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try{
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception ex){
//do nothing...
}
try{
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception ex){
//do nothing...
}
return gps_enabled || network_enabled;
}
Then finally to open if location service in turned off previously
if (isLocationServiceEnabled())) {
//DO what you need...
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Seems Like location service is off, Enable this to show map")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}).setNegativeButton("NO THANKS", null).create().show();
}
You should use the Location Settings Dialog in Play Services that prompts the user to enable location services (if necessary) with just one click.
if your question is at the user level of android these properties are located in: "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites"
.
But at the developer can use the class "android.provider.Settings.Secure"
with the appropriate permissions.
-
This is not the answer, the person who ask the question wanted – Omar Faroque Anik Oct 14 '14 at 19:13