Starting from ‘FROYO’ 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.

出处
http://stackoverflow.com/questions/3644144/how-to-disable-mobile-data-on-android

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()