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

11,178

Solution 1

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.

Solution 2

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.

Share:
11,178
Taimur Ayaz
Author by

Taimur Ayaz

Updated on August 21, 2022

Comments

  • Taimur Ayaz
    Taimur Ayaz over 1 year

    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);
     }
    } 
    
  • Taimur Ayaz
    Taimur Ayaz almost 11 years
    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.
  • CommonsWare
    CommonsWare almost 11 years
    Fortunately, your GPS hack relies on a security flaw that has been fixed for quite some time.
  • Oam
    Oam almost 11 years
    @CommonsWare Thanks for mentioning, I will correct it in my future scenarios.
  • amithgc
    amithgc almost 11 years
    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!
  • TacB0sS
    TacB0sS over 10 years
    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...:)?
  • CommonsWare
    CommonsWare over 10 years
    @TacB0sS: I have no idea regarding mobile data.
  • TacB0sS
    TacB0sS over 10 years
    I'll give it a go later, and post a response... Thanks
  • TacB0sS
    TacB0sS over 10 years
    On that same notion, the Device locale change api has been also restricted, since 4.2
  • tejas
    tejas over 10 years
    I tried this, it is just showing that "Searching for GPS" but not actually turned it ON
  • sijo jose
    sijo jose over 8 years
    it show gps icon on notification bar but it not actually turn on gps