How to Check Airplane Mode State in Android Programmatically? - GeeksforGeeks

28 Jul, 2021
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 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.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <Button        android:id="@+id/Check"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Check"        android:layout_centerInParent="true"/>  </RelativeLayout>
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.
Kotlin
import android.os.Buildimport android.os.Bundleimport android.provider.Settingsimport android.widget.Buttonimport android.widget.Toastimport androidx.annotation.RequiresApiimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)          override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Find the button from the layout file        // and add functionality to it        val btn = findViewById<Button>(R.id.Check)        btn.setOnClickListener {                        // Get the value of AIRPLANE MODE             // from the system settings.            // This is an Integer. Airplane Mode             // is ON is its 1 and 0 otherwise.            // Toast the message (off/on)            if(Settings.System.getInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) == 0){                Toast.makeText(applicationContext,"Off",Toast.LENGTH_SHORT).show()            } else {                Toast.makeText(applicationContext,"On",Toast.LENGTH_SHORT).show()            }        }    }}
Input:
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.
Output:
Useful Tip:
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.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Retrofit with Kotlin Coroutine in Android
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters