How to programmatically enable GPS in Android Cupcake
Asked Answered
M

7

28

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?

Mariammarian answered 26/6, 2009 at 23:1 Comment(1)
depends on the version of Cupcake. 1.5 doesnt allow it apparently.Polynices
I
51

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.

Inhuman answered 27/6, 2009 at 0:33 Comment(7)
Why 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?Propinquity
It is disabled for privacy reasons. If the user wants GPS off, the user should have GPS off, period.Inhuman
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/…Spin
@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 TaskerConfectioner
@RichardLeMesurier: Such security loopholes have been fixed, at least for newer versions of Android.Inhuman
@commonsware That is correct - fixed from 2.3 onwards (as at time of writing 20/06/2012). However some developers require a quick-n-dirty way of doing something, so I like to ensure they can find the hacks when needed.Confectioner
@RichardLeMesurier: I like to ensure that users can maintain their privacy and security, by maintaining complete control over whether GPS is enabled.Inhuman
B
15
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
    startActivity(myIntent);
}
Barbital answered 23/7, 2010 at 2:22 Comment(1)
This 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); }Tivoli
I
6

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();
  }
}
Importune answered 29/9, 2011 at 12:34 Comment(1)
yes this can be done up to 2.2 (sdk 8). For more info, see Enable GPS programatically like TaskerConfectioner
S
3

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 :/

Stockman answered 29/4, 2011 at 19:45 Comment(0)
S
1

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();
                }
Showplace answered 4/3, 2016 at 18:2 Comment(0)
D
0

You should use the Location Settings Dialog in Play Services that prompts the user to enable location services (if necessary) with just one click.

Doukhobor answered 15/6, 2015 at 17:10 Comment(0)
P
-5

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.

Prussian answered 25/1, 2011 at 9:3 Comment(1)
This is not the answer, the person who ask the question wantedAugmentative

© 2022 - 2024 — McMap. All rights reserved.