0

Possible duplicates,

How to enable/disable gps and mobile data in android programmatically?

I have been seeing people telling that they are able to turn ON the GPS in android programatically. But I'm using the same code and not able to do that.

It is just showing that "Searching for GPS .." but not actually doing it.

Here is the code I'm using,

  //Enable GPS
  Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
  intent.putExtra("enabled", true);
  sendBroadcast(intent);

  //Disable GPS
 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
 intent.putExtra("enabled", false);
 sendBroadcast(intent);

I also tried this,

    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);
    }

But when I do,

 LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE );
 boolean isGPSEnabled = manager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

it returns me isGPSEnabled as false always. If I turn on the gps manually it is returned as true.

Can any body tell me is it possible to ON the GPS like this? If so, where I'm going wrong.

Community
  • 1
  • 1
tejas
  • 2,435
  • 10
  • 37
  • 54
  • Q:Where are am I going wrong? A:In knowing that the question is a duplicate but going ahead and posting it anyway. – NickT Dec 23 '13 at 09:29

1 Answers1

3
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);

//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);

use context when you broadcast.

hope this works..

To enable mobile data you should use this -

final ConnectivityManager conman = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass
            .getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField
            .get(conman);
    final Class iConnectivityManagerClass = Class
            .forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass
            .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
Priyanka
  • 677
  • 5
  • 20
Amit
  • 391
  • 3
  • 15
  • I am using the first code only but without context. It is not working – tejas Dec 23 '13 at 10:14
  • it should work when you will use context,please try using context. – Amit Dec 23 '13 at 10:16
  • I tried using getApplicationContext().sendBroadcast(intent); If I turn ON GPS manually, disabling works but not enabling. It just says that Searching for GPS – tejas Dec 23 '13 at 10:36
  • please check whether you are able t get latitude and longitude if you are getting then it is working fine – Amit Dec 23 '13 at 10:47
  • No, I am not able to get that. After I enable this, I tried LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE ); boolean isGPSEnabled = manager .isProviderEnabled(LocationManager.GPS_PROVIDER); but it is returning false. and when I do location = manager .getLastKnownLocation(LocationManager.GPS_PROVIDER); location is null – tejas Dec 23 '13 at 10:48
  • Check your device has working data connection? and ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION Permission required – Amit Dec 23 '13 at 10:59
  • Where can I find them? :( I am not able to see – tejas Dec 23 '13 at 11:03
  • I am using Galaxy S3, I am not able to find that here :( – tejas Dec 23 '13 at 11:09
  • pull down your notification bar you will se wifi and mobile data option on wifi if you have wifi connection or else on mobile data then try to run same code – Amit Dec 23 '13 at 11:23
  • pull down your notification bar you will se wifi and mobile data option on wifi if you have wifi connection or else on mobile data – Amit Dec 23 '13 at 11:23
  • you need permission as well – Amit Dec 23 '13 at 11:32
  • I have set all the permissions. And I am using this on ICS+. I have the wifi ON – tejas Dec 23 '13 at 11:34
  • In the Location Services, if I turn ON Use GPS satellites manually, it works. But this is not what I want – tejas Dec 23 '13 at 11:35
  • Did this code work for you in Jelly Bean? – tejas Dec 23 '13 at 11:47
  • yes for me it works fine in every version, i am still check it if i can produce the issue – Amit Dec 23 '13 at 11:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43753/discussion-between-amit-and-tejas) – Amit Dec 23 '13 at 11:51
  • Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0. However, you can change wifi off on programmatically – Gaurav Vachhani Jul 30 '15 at 08:42
  • check out the ola mobile app :) – Amit Aug 26 '15 at 08:16