How to switch to airplane mode or power off Android in the night.

First of all, if you want to switch to airplain mode automatically, then you need a rooted device. It’s mandatory despite of what you will use, and in this post we will need to be root for every step.

You may download an application as the one that I had, Auto Airplain; however, that app stopped working in my cellphone and besides contained ads. I had tried many applications for this task and Auto Airplain was the only one that worked.
Notice that Auto Airplain is not FOSS, remember that it’s important to choose FOSS apps that respect your freedom. Here of course you will learn how to switch to airplane using FOSS software, and this method certainly will work. And so, you should consider use Linage OS instead of Android if you are not using it.

Why would anyone want to enable the airplane mode in the night? Because being offline will help you to rest properly, and you will save energy. Now, it will be discussed why to prefer the Airplain mode over powering off the device.
And I think that this topic should be of the interest of everyone in the world. In this post, you also will learn how to power off your cellphone in a scheduled time.

Why to switch to Airplain mode instead of powering off it?

It’s not efficient to shutdown the android phone in the night, first you won’t able to use it as alarm, second you may forget to power it on, and finally you will face a bunch of notifications in the morning at the same time that many services are starting, such an awkward and awful start up.

With regard of VPN, it’s a good idea to keep the daemon running all the time since in the start up process many applications will manage to start up before openvpn.

Also, if your cellphone is encrypted, it will take longer the boot process, a smartphone is designed to be swift and ready.

As for the second factor authentication, you won’t be able to log in your web services immediately.

Requirements:

 

  1. Terminal, I recommend Termux, if you are going to use another terminal emulator, then make sure you have a full featured keyboard as the hacker keyboard, or connect a PC’s keyboard to the device via USB.
  2. Busybox
  3. Know how to write a crontab file
  4. Know how to edit with vi.

Procedure:

Difficulty: Medium Rooting the device and managing some simple commands.

Install crond from F-droid, else you can search in a search engine for F-droid crond and download the APK from the F-droid site.
Open a terminal and execute:
su -c ‘vi /data/crontab’

It will open the vi editor.

Very brief introduction to vi

Vi is a powerful editor for POSIX systems (Mac OS X, Linux, Android, etc.), indeed I wrote this post using vim, which is a vi improved.
Vi has two modes: The edit mode, and the command mode, vi start in the command mode, so to start editing you must press the i key.
When you end your editing you must press the escape button, and write :wq, then press enter.

The escape button can be emulated in Termux using the volume up button plus the “e”.

Crontab

Cron is a daemon that works in the Unix systems (Mac OS X, Linux, FreeBSD) for scheduling task, however, Apple deprecated cron in Mac Os. This crond utility for Android mimics the functioning of cron but it is not an authentic cron.

If you do not how to edit a crontab, then you have to read the cron article in Wikipedia.

There we can start programming our crontab with the Android command line, and we require 3 commands for enabling the airplain mode, so you will have to add 6 entries, 3 for entering into the airplain mode, and 3 for disabling the airplain mode in the morning:

Notice that svc is a very useful adb shell command, and settings allows to edit all the Android settings from the command line.

The commands for enabling the airplain mode:
svc data disable # It will disable the mobile data
svc wifi disable # It will disable the wifi
settings put global airplane_mode_on 1 # It will enable the airplain mode

Commands for disabling the airplain mode:
svc data enable
svc wifi enable
settings put global airplane_mode_on 0

If you want to poweroff your device instead of switching to the airplain mode, just use this command:
poweroff -n

Now an example, if you want to enable the airplain mode an the 10:30PM and disable it at the 6:30AM from Monday to Friday, your crontab would be as follows:

30 22 * * 1-5 svc data disable
30 22 * * 1-5 svc wifi disable
30 22 * * 1-5 settings put global airplane_mode_on 1
31 6 * * 1-5 svc data enable
31 6 * * 1-5 svc wifi enable
30 6 * * 1-5 settings put global airplane_mode_on 0

Very important! The svc commands has to be execute one minute after disabling the airplain mode with the  settings command, just as in the example.

Instead, if you only want to only power off your cellphone at the 10:30PM all the days, your crontab will be only this line:
30 22 * * * poweroff -n -f

It all is pretty easy.

These bottons respect your privacy