Toggle on/off GPS in Android

Posted: June 3, 2012 in Android
Tags: ,

Hello folks, here am again with android simple code of GPS toggle on/off from code.
what you have to do is just copy the following code in Global.java (where you set your global variables and functions)

when you need to turn on GPS, just call Global.turnGPSOn(“your context”)

here, “your context” will be the class file from where you are calling this function,
let say, i have a class file, named “SplashActivity.java” so what i have to pass as a parameter in that function is,
Global.turnGPSOn(SplashActivity.this);

you can also use, getApplicationContext() as a parameter. But I recommend to use class file name.

Now here is the code,

public static void turnGPSOn(Context context){
        String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if(!provider.contains("gps"))
{ //if gps is disabled
            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")); 
            context.sendBroadcast(poke);
        }
    }
    
    public static void turnGPSOff(Context context){
        String provider = Settings.Secure.getString(context.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")); 
            context.sendBroadcast(poke);
        }
    }    

we heard and face so many times that our phone battery is drawn and all that, its due to constant pinging of GPS from any application, which will cause the battery issues. So when you need to fetch location from GPS provider, just turn on the GPS from code and after using it, turn off it.

Advertisements
Comments
  1. Jasjit says:

    Hi,

    I appreciate the code since I’ve been trying to do this for a long time but for some reason it’s not working for me. I do not get an error but when I try to toggle it, the GPS settings do not change. LogCat doesn’t give any errors either, and I’ve given the required permissions in Manifest. Any idea what the problem could be?

    • Hi Jasjit, thanks for appreciating. You need to add permissions in manifest as below,

      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  2. Jasjit says:

    Thanks for the fast reply Rahul. I’ve added the permissions but it’s still not working for some reason. If it was a problem with permissions, I think I would get an error, but I am not getting any error. Just nothing is happening. I’ve tested all the other parts of my code to make sure they work 100% so I don’t know why it’s not working.

    • Hello jasjit, I checked it on my different devices (on Froyo and Gingerbread), it working well. Can you please tell me the exact scenario, where you use this. and on which os (Android Version)? I will check this on ICS also and post it here also.

  3. Jasjit says:

    Ah, yes, that could be the problem. I’ve only been testing on Android ver. 4.0.3. I’ve tested on the emulator as well as a real device (Galaxy Note N-7000) running on a custom ICS ROM. I’m actually trying to create an automation application, a little bit like Tasker. So I basically want to give the user the option to disable or enable GPS when the screen turns on, turns off, an application opens, etc. etc. I am using a broadcast receiver to detect when the screen unlocks and I have made sure all the other toggles are working correctly (Airplane Mode, Wi-Fi, Bluetooth, etc.) but GPS was purposely removed by the Android devs back in Android 1.5 so we have to use workarounds like the one you’ve posted here. I’ve seen something similar elsewhere but I never could get it to work. I saw your code is very complete and polished so I thought I’d give it a try. It’s possible they’ve removed the ability to do this in ICS, I’ll try my app on a Gingerbread emulator.

    Thanks again for all of your help. Let me know if you need anything else (relevant code, etc.).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s