Auto switch off data android

I was looking for an application that can auto switch off my data connection as soon as I switch the screen of my android device off. The thing is that I don't care about data sync, I just need my phone to receive SMS & calls while the screen is off.

It is, from my point of view, really anoying to scroll down the notification status bar and enable/disable the data each time I turn on the screen. After all, I am a developer and the less I do, the better I feel :p

So, I starting digging around google to find if I can script it.

First, my android device is rooted, which is really great because I can run root command from adb shell. The first command I found is to enable or disable the data:

svc data disable


svn data enable


Then, I found another command to get the screen status:

dumpsys power|grep "Display Power: state="


With busybox installed, I can do better by awk-ing the result in order to have only the on/off status:

dumpsys power |grep "Display Power: state=" | busybox awk -F'=' '{print $2}'

Finally, bringing all of this in a script which run continuously:

while [ 1 ] ; do
 STATUS=$(dumpsys power |grep "Display Power: state=" | busybox awk -F'=' '{print $2}')
 if [ "Z$STATUS" = "ZOFF" ] ; then
  echo "$DATE   --  disabling data"
  svc data disable
  echo "$DATE   --  enabling data"
  svc data enable
sleep 5

It works!

I choose for now not to run this script automatically, but from a terminal emulator in order to test it few days/weeks, but as far as I can see, it's working very smoothly!

#android #adb #data