How to Check Airplane Mode State in Android Programmatically?
Airplane Mode is often seen in action during flights, avoiding calls, or rebooting the network on mobiles, tablets, and laptops. Airplane mode is a standalone mode where the device turns down the radio communications. These may include Wifi, GPS, Telephone Network, Hotspot depending upon the year of manufacture, brand, and location of purchase of the device, but not the Bluetooth. Applications supporting streaming might require internet connectivity and needs to stay updated on the state of Internet Connection. While Airplane Mode impacts internet connectivity, such applications would require alternatives to handle such an instance. Through this article, we will show you how you could programmatically check the Airplane Mode State.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Create a Button in the layout file.
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail. Below is the code to check if Airplane Mode State.
Check if the Airplane Mode is turned off. Now click the button. We shall get a message that it is turned off. Now turn on the Airplane Mode. Return to the activity and again click the button. We shall get a message that it is turned on.
Note that we cannot change the state of the Airplane Mode programmatically. We have a provision to change it only if the application is a System Application. This can be done by adding permission to write settings in the Manifest file and calling a putInt() function in the Settings.System.