Programmatically Toggle "restrict Background Data"
If I go to 'Settings - Data Usage' and press the 'Properties' I can activate 'Restrict Background Data', using a Samsung Galaxy S2 (i9105P) with Android 4.1.2. Is there any way I c
Solution 1:
You can run this command in the command line
svc data disable
or svc data enable
You obviously need root to do that, like this:
Runtime.getRuntime().exec("echo svc data disable | su");
This should bring up root dialog if your device is rooted.
Solution 2:
As I know for Android 4.x you cant do that. Only monkey runner plugin can help you.
But if you need for Android 2.x, this is method I used:
/**
* Switch mobile data network access
* */publicvoidnmSwitchMobileNetworkDataAccess(boolean swtichCellOn){
boolean disable;
TelephonyManagertelephonyManager= (TelephonyManager)m_context.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
disable = false;
}else{
disable = true;
}
try{
finalConnectivityManagerconman= (ConnectivityManager)m_context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
finalMethodsetMobileDataEnabledMethod= 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() );
}
elseif(disable == false && swtichCellOn == false){
setMobileDataEnabledMethod.invoke(conman, false);//turn cell off
DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("Turn cell off, done",
EMethodResponse.ON_NM_REQ.FromEnumToString() );
}
elseif((disable == false && swtichCellOn == true) || (disable == true && swtichCellOn == false)){
DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("No changes",
EMethodResponse.ON_NM_REQ.FromEnumToString() );
}
}
catch(Exception e){
e.printStackTrace();
}
}
Post a Comment for "Programmatically Toggle "restrict Background Data""