Programmatically Toggle "Limit Source Data"

If I go to “Settings - data usage” and click “Properties”, I can activate “Limit background data” using Samsung Galaxy S2 (i9105P) with Android 4.1.2.

Is there a way to do this programmatically, both on and off?

I want to activate / deactivate it only under certain conditions (determined by my application), so I do not need to manually activate it.

PS: I searched the website android.developer.com, but did not succeed.

+4
source share
2 answers

As I know for Android 4.x, you cannot do this. Only a monkey plugin can help you.

But if you need for Android 2.x, this is the method I used:

/** * 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(); } } 
0
source

You can run this command at the command line svc data disable or svc data enable To do this, you obviously need root:

Runtime.getRuntime().exec("echo svc data disable | su");

This will cause a root dialog to appear if your device is rooted.

0
source

Source: https://habr.com/ru/post/1501165/


All Articles