Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Mobile Data on Android

Tags:

android

Quick back story before someone tells me to buy an app. =)

I just got an EVO and it chews through the battery fairly quick. I downloaded JuiceDefender to manage my mobile data connection. That seems have worked out fairly well. However, the settings are just very restricted (even on the paid versions).

As of right now I am trying to develop a much more customizable battery saving application. The main thing I am trying to do first be able to enable/disable the mobile data connection at will.

The problem is I can't find any code snippets or articles on how to do this. The only thing I have found is the following. I don't know how accurate this is, but this was all I could piece together 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 can point me to anything that could help, it would be most appreciated.

UPDATE

It appears that the HTC Evo on Sprint does not use APN settings. I tested this by downloading APNDroid and watching it not work. I then made a quick app to dump all APN entries to the screen. That yielded one result and it was for mms.

Looking at the phone info when JuiceDefender is running, I found that the GSRP network is getting turned on and off. This leaves me to believe it is possible to do it through code even though every page I find asking about this same issue says it cannot be done. The kicker is they all say to do it like APNDroid. Please someone give me some insight.

Thanks!

like image 959
TyCobb Avatar asked Sep 04 '10 21:09

TyCobb


People also ask

Can I disable mobile data?

You can turn off cellular data on an Android device to avoid hitting your data cap. You can swipe down from the top of the screen and disable cellular data with a single tap. If you prefer, you can disable data for specific apps, such as streaming video apps that use a lot of data.

What does it mean to disable mobile data?

If you disable your mobile data, you can only access the Internet via Wi-Fi.. If no Wi-Fi is available or if your mobile device isn't connected to a Wi-Fi network, disabling your mobile data means: You will no longer receive updates related to your device, email or mobile's proper operation.


2 Answers

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

With this method you can change the system setting: 'Settings -> Wireless & network -> Mobile network settings -> Data Enabled'

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 the CHANGE_NETWORK_STATE permission.

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

Needless to say that this approach might not work in future android versions. But I guess that applications such as '3G watchdog', 'APNdroid' or 'DataLock' work this way.


UPDATE:
setMobileDataEnabled method is no longer available on Lollipop

like image 61
Vladimir Sorokin Avatar answered Sep 22 '22 16:09

Vladimir Sorokin


The Dataconnection disable and enabling APIS are hidden in the SDK and not exposed to the user, this can be achived 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); 
like image 36
Vinay Avatar answered Sep 25 '22 16:09

Vinay