据我所知,对于 Android 4.x,您不能这样做。只有猴子跑步者插件可以帮助你。
但如果您需要 Android 2.x,这是我使用的方法:
/**
* Switch mobile data network access
* */
public void nmSwitchMobileNetworkDataAccess(boolean swtichCellOn){
boolean disable;
TelephonyManager telephonyManager = (TelephonyManager)m_context.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
disable = false;
}else{
disable = true;
}
try{
final ConnectivityManager conman = (ConnectivityManager)m_context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final Method setMobileDataEnabledMethod = conman.getClass().getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
if(disable == true && swtichCellOn == true){
setMobileDataEnabledMethod.invoke(conman, true);//turn cell on
DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("Turn cell on, done",
EMethodResponse.ON_NM_REQ.FromEnumToString() );
}
else if(disable == false && swtichCellOn == false){
setMobileDataEnabledMethod.invoke(conman, false);//turn cell off
DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("Turn cell off, done",
EMethodResponse.ON_NM_REQ.FromEnumToString() );
}
else if((disable == false && swtichCellOn == true) || (disable == true && swtichCellOn == false)){
DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("No changes",
EMethodResponse.ON_NM_REQ.FromEnumToString() );
}
}
catch(Exception e){
e.printStackTrace();
}
}