How to Check Airplane Mode State in Android Programmatically?
Last Updated :
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 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.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
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.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import 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)
val btn = findViewById<Button>(R.id.Check)
btn.setOnClickListener {
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...