Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

How to disable Mobile Data on 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!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...