AIRPLANE MODE PROGRAM
AIRPLANE MODE PROGRAM
XML Code
xml
CopyEdit
xml
CopyEdit
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
xml
CopyEdit
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textAlignment="center"
android:id="@+id/tvStatus"
/>
xml
CopyEdit
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:text="Check Status"
android:textSize="30dp"
android:id="@+id/btnCheckStatus"
/>
2. AirplaneModeReceiver.java
Java Code
java
CopyEdit
package com.example.broadcastreciever;
Declares the package name (the unique identifier for this app).
java
CopyEdit
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
java
CopyEdit
java
CopyEdit
@Override
java
CopyEdit
if(intent.getAction()!=null &&
intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
java
CopyEdit
java
CopyEdit
if(isAirplaneMode){
Toast.makeText(context, "Airplane Mode is Activated", Toast.LENGTH_SHORT).show();
} else {
3. MainActivity.java
Java Code
java
CopyEdit
package com.example.broadcastreciever;
java
CopyEdit
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
java
CopyEdit
java
CopyEdit
Button checkStatus;
java
CopyEdit
@Override
CopyEdit
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
java
CopyEdit
statusText = findViewById(R.id.tvStatus);
checkStatus = findViewById(R.id.btnCheckStatus);
java
CopyEdit
checkStatus.setOnClickListener(new View.OnClickListener() {
@Override
java
CopyEdit
checkAirPlaneModeStatus();
java
CopyEdit
java
CopyEdit
boolean isAirplaneModeOn =
Settings.Global.getInt(getContentResolver(),Settings.Global.AIRPLANE_MODE_ON,0)!=0;
statusText.setText(status);
java
CopyEdit
@Override
super.onDestroy();
unregisterReceiver(airplaneModeReceiver);
XML Code
xml
CopyEdit
<receiver android:name=".AirplaneModeReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE_CHANGED"/>
</intent-filter>
</receiver>
Final Thoughts
✔ Detects Airplane Mode changes in real-time.
✔ Displays the current Airplane Mode status.
✔ Registers and unregisters the receiver properly.
4o
You said:
ChatGPT said:
Certainly! Let's break down this code line by line to understand what exactly is happening:
Code:
java
CopyEdit
registerReceiver(airplaneModeReceiver, filter);
java
CopyEdit
java
CopyEdit
What is IntentFilter?
An IntentFilter tells Android which broadcasts this receiver should listen for.
Intent.ACTION_AIRPLANE_MODE_CHANGED
java
CopyEdit
registerReceiver(airplaneModeReceiver, filter);
Breaking it down:
registerReceiver(airplaneModeReceiver, filter);
o Second Parameter (filter) → Defines which broadcast actions the receiver should
listen for.
3. The registered AirplaneModeReceiver detects this change and executes its onReceive()
method.
4. The onReceive() method checks the new state and displays a toast message.
Important Notes
xml
CopyEdit
<receiver android:name=".AirplaneModeReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE_CHANGED"/>
</intent-filter>
</receiver>
o The difference:
Dynamic Receivers (like this one) → Active only when registered in code.
java
CopyEdit
@Override
super.onDestroy();
unregisterReceiver(airplaneModeReceiver);
Summary