4

I want my application to be able to enable/disable gps and mobile data programmatically as there are many apps like tasker, profile flow, lookout extra which can do this so I searched for it but not found any useful example I found the following code but they didn't work.

private void setMobileDataEnabled(Context context, boolean enabled) {
 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);
} 

private void turnGPSOn(){
 String provider = Settings.Secure.getString(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")); 
    sendBroadcast(poke);
 }
}

private void turnGPSOff(){
 String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 if(provider.contains("gps")){ //if gps is enabled
    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);
 }
} 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Taimur Ayaz
  • 312
  • 1
  • 6
  • 18
  • What android version are you using for testing? – Oam May 25 '13 at 19:11
  • android 2.3 and 4.1.2 – Taimur Ayaz May 25 '13 at 19:12
  • Try the below code, let me know if that worked. – Oam May 25 '13 at 19:19
  • its kind of working thanks but what it is doing is turning on the service so if have started it using my app a icon appears on top of the status bar and it is searching for gps......and if i even turn it off manually or from the app it does not go from there and on android 4.1.2 it didnt turned on the gps icon in the status bar but it started the service but in android 2.3 it turned on the icon and also started the service – Taimur Ayaz May 25 '13 at 19:37
  • Forget GPS... were you able to disable the mobile data at least? If yes, how... does the code (other than that for the GPS) work? Also, please mention other changes needed like namespaces etc.... – who-aditya-nawandar Nov 12 '13 at 09:38

2 Answers2

4

I want my application to be able to enable/disable gps

Fortunately, this is not possible, for obvious privacy reasons. While there were some hacks, like the one in your question, that used to work, those security flaws have since been fixed.

as there are many apps like tasker, profile flow, lookout extra which can do this

You are welcome to supply evidence that any of those apps can do this on modern versions of Android.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • im the developer of Profile Flow.. and regarding this discussion, controlling/changing GPS setting is not possible in the latest versions of android.. unless it is rooted! – amithgc Jun 17 '13 at 13:39
  • Since The question was also about mobile network, and since you have not mention nothing of it, I assume this feature is still in place for all us commoners...:)? – TacB0sS Aug 04 '13 at 14:50
  • @TacB0sS: I have no idea regarding mobile data. – CommonsWare Aug 04 '13 at 14:51
  • I'll give it a go later, and post a response... Thanks – TacB0sS Aug 04 '13 at 15:02
  • On that same notion, the Device locale change api has been also restricted, since 4.2 – TacB0sS Aug 15 '13 at 21:13
1

Try this for turning on gps

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}

for turning off gps,

public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider.contains("gps")){ //if gps is enabled
        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")); 
        this.ctx.sendBroadcast(poke);
    }
}

For mobile data, did you added the permission in manifest? If not try adding and check it.

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

Let me know if everything worked.

Oam
  • 912
  • 6
  • 7
  • its kind of working thanks but what it is doing is turning on the service so if have started it using my app a icon appears on top of the status bar and it is searching for gps......and if i even turn it off manually or from the app it does not go from there and on android 4.1.2 it didnt turned on the gps icon in the status bar but it started the service but in android 2.3 it turned on the icon and also started the service. – Taimur Ayaz May 25 '13 at 19:32
  • Fortunately, your GPS hack relies on a security flaw that has been fixed for quite some time. – CommonsWare May 25 '13 at 21:40
  • @CommonsWare Thanks for mentioning, I will correct it in my future scenarios. – Oam May 26 '13 at 08:06
  • I tried this, it is just showing that "Searching for GPS" but not actually turned it ON – tejas Dec 23 '13 at 09:09
  • it show gps icon on notification bar but it not actually turn on gps – sijo jose Dec 22 '15 at 07:26