How to disable mobile data on Android

A quick story back before someone tells me to buy the app. =)

I just have an EVO and it chews the battery pretty quickly. I downloaded JuiceDefender to manage my mobile data connection. It seems to have worked out quite well. However, the settings are very limited (even in paid versions).

I'm currently trying to develop a much more customizable application to save battery. The main thing I'm trying to do is to first enable / disable the mobile data connection at your discretion.

The problem is that I cannot find code snippets or articles on how to do this. The only thing I found is the following. I don’t know how accurate this is, but that was all I could get together by browsing developer.android.com

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE); cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "android.net.conn.CONNECTIVITY_CHANGE"); State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); textView.setText(state.name()); 

If anyone could point me to anything that could help, that would be very appreciated.

UPDATE

It seems that HTC Evo on Sprint does not use APN settings. I tested this by downloading APNDroid and watching how it does not work. Then I made a quick application to reset all APN entries to the screen. This gave one result, and that was for mms.

Looking at the phone information when JuiceDefender is running, I find that the GSRP network is turning on and off. This leads me to believe that this can be done using code, although every page I find asks about the same problem, says that this is not possible. Kicker - they all say to do it like APNDroid. Please give me some idea.

Thank!

+42
android
Sep 04 '10 at 21:57
source share
7 answers

Disabling and enabling APIS Dataconnection are hidden in the SDK and not exposed to the user, this can be achieved by accessing the ITelephony interface using the Java reflection technique.

here you go:

  Method dataConnSwitchmethod; Class telephonyManagerClass; Object ITelephonyStub; Class ITelephonyClass; TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){ isEnabled = true; }else{ isEnabled = false; } telephonyManagerClass = Class.forName(telephonyManager.getClass().getName()); Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); getITelephonyMethod.setAccessible(true); ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName()); if (isEnabled) { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("disableDataConnectivity"); } else { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("enableDataConnectivity"); } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub); 
+38
Nov 29 '10 at 13:18
source share

Starting with "Gingerbread" you can use the IConnectivityManager.setMobileDataEnabled () method. It is hidden in the API, but is available with reflection . http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/net/ConnectivityManager.java#376

Using this method, you can change the system parameter: "Settings β†’ Wireless and network β†’ Mobile network settings β†’ Data is included

Code example:

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

Also you need permission CHANGE_NETWORK_STATE .

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

Needless to say, this approach may not work in future versions of Android. But I think apps like Watchdog 3G, APNdroid, or DataLock work that way.




UPDATE :
The setMobileDataEnabled method is no longer available on Lollipop

+73
Jan 22 '12 at 15:35
source share

Switching the mobile data network by changing the APN name does not work already with Gingerbread. And although the reflection code is probably the right way to do the trick, it won’t work, because the application requires android.permission.MODIFY_PHONE_STATE permission, as Alex P. explained, otherwise you will get this nasty exception:

 03-18 21:54:55.074: WARN/System.err(1851): java.lang.reflect.InvocationTargetException (...) 03-18 21:54:55.263: WARN/System.err(1851): Caused by: java.lang.SecurityException: Neither user 10037 nor current process has android.permission.MODIFY_PHONE_STATE. (...) 03-18 21:54:55.303: WARN/System.err(1851): at com.android.internal.telephony.ITelephony$Stub$Proxy.disableDataConnectivity(ITelephony.java:888) 

Unfortunately, you cannot set this permission because it is a level 3 permission that is not allowed for applications:

 03-18 21:48:39.334: WARN/PackageManager(75): Not granting permission android.permission.MODIFY_PHONE_STATE to package XXX (protectionLevel=3 flags=0x8be46) 

I don’t think anyone has a way to override resolution suppression, except using their own firmware.

+4
Mar 18 2018-11-18T00:
source share

I think there are two main types of mobile data connection on an Android device: WiFi and 3G / HSDPA / etc.

And afaik, you should be able to disable WiFi programmatically, but I think that 3G / HSDPA / etc connections can only be disconnected by changing the APN name. The reason I say this is because it is a popular APNDroid application.

+3
Sep 05 '10 at 15:41
source share

Please note that the "Android version of .MODIFY_PHONE_STATE" is no longer supported for Android 2.3 and higher.

why is the 2.3 version of android not hava android.permission.MODIFY_PHONE_STATE? and what is the solution for this?

+3
Apr 18 2018-11-11T00:
source share

To add a toggle button, you can use this code additionally. Vladimir Answer:

 TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); switch (telephonyManager.getDataState()) { case TelephonyManager.DATA_CONNECTED: setMobileDataEnabledMethod.invoke(iConnectivityManager, false); break; case TelephonyManager.DATA_DISCONNECTED: setMobileDataEnabledMethod.invoke(iConnectivityManager, true); break; } 

Workaround for this problem still works for me on Android 4.0.4

+3
Jun 27 '12 at 11:01
source share

@Mariux: you probably forgot to add <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> to AndroidManifest.xml

0
Feb 15 '11 at 15:44
source share



All Articles