0% found this document useful (0 votes)
32 views

AIRPLANE MODE PROGRAM

The document outlines the implementation of an Android app that detects and displays the status of Airplane Mode using a BroadcastReceiver. It includes the layout file for the user interface, the Java code for the BroadcastReceiver and main activity, and the registration of the receiver in the AndroidManifest.xml. The app allows users to check the Airplane Mode status and provides real-time notifications when the mode changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

AIRPLANE MODE PROGRAM

The document outlines the implementation of an Android app that detects and displays the status of Airplane Mode using a BroadcastReceiver. It includes the layout file for the user interface, the Java code for the BroadcastReceiver and main activity, and the registration of the receiver in the AndroidManifest.xml. The app allows users to check the Airplane Mode status and provides real-time notifications when the mode changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

AIRPLANE MODE PROGRAM (BROADCAST RECEIVER)

1. Layout File (activity_main.xml)

This defines the User Interface (UI) of your app.

XML Code

xml

CopyEdit

<?xml version="1.0" encoding="utf-8"?>

 Declares this file as an XML document.

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">

 Defines a LinearLayout (a container that arranges views vertically or horizontally).

 xmlns:android="..." → XML namespace for Android attributes.

 android:id="@+id/main" → Assigns an ID to this layout.

 android:layout_width="match_parent" → Layout takes full screen width.


 android:layout_height="match_parent" → Layout takes full screen height.

 android:orientation="vertical" → Children will be arranged vertically.

 android:gravity="center" → Aligns all child elements to the center.

xml

CopyEdit

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="30dp"

android:textAlignment="center"

android:text="Check Airplane Mode Status: "

android:id="@+id/tvStatus"

/>

 TextView → Displays text on the screen.

 android:text="Check Airplane Mode Status: " → Default text message.

 android:textSize="30dp" → Sets font size.

 android:textAlignment="center" → Aligns text in the center.

 android:id="@+id/tvStatus" → Assigns an ID to reference in Java.

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"

/>

 Button → Clickable button for checking airplane mode.

 android:text="Check Status" → Label for the button.

 android:layout_margin="40dp" → Adds space around the button.

 android:id="@+id/btnCheckStatus" → Assigns an ID for reference in Java.

2. AirplaneModeReceiver.java

This is a BroadcastReceiver that listens for Airplane Mode changes.

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;

 BroadcastReceiver → Listens for system-wide events like Airplane Mode change.

 Context → Provides access to app-specific resources.


 Intent → Stores information about the event.

 Toast → Displays a popup message.

java

CopyEdit

public class AirplaneModeReceiver extends BroadcastReceiver {

 Defines a new class called AirplaneModeReceiver that extends BroadcastReceiver.

java

CopyEdit

@Override

public void onReceive(Context context, Intent intent) {

 onReceive() → Called when Airplane Mode changes.

java

CopyEdit

if(intent.getAction()!=null &&
intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){

 Checks if the received event is "AIRPLANE_MODE_CHANGED".

java

CopyEdit

boolean isAirplaneMode = intent.getBooleanExtra("state", false);

 Retrieves the new Airplane Mode state:

o true → Airplane Mode is ON.

o false → Airplane Mode is OFF.

java

CopyEdit

if(isAirplaneMode){
Toast.makeText(context, "Airplane Mode is Activated", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(context, "Airplane Mode is Deactivated", Toast.LENGTH_SHORT).show();

 Shows a toast notification when Airplane Mode is toggled.

3. MainActivity.java

This is the main activity that users interact with.

Java Code

java

CopyEdit

package com.example.broadcastreciever;

 Declares the package name.

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;

 Imports required classes:

o IntentFilter → Specifies which broadcasts to listen for.

o Settings → Allows checking Airplane Mode status.

o TextView, Button, Toast → UI components.

o AppCompatActivity → Base class for activities.

java

CopyEdit

public class MainActivity extends AppCompatActivity {

 Defines the main activity.

java

CopyEdit

private AirplaneModeReceiver airplaneModeReceiver;

private TextView statusText;

Button checkStatus;

 Declares variables for:

o airplaneModeReceiver → The receiver that listens for changes.

o statusText → Displays Airplane Mode status.

o checkStatus → The button to check status.

java

CopyEdit

@Override

protected void onCreate(Bundle savedInstanceState) {

 Called when the activity starts.


java

CopyEdit

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 Sets up the layout.

java

CopyEdit

statusText = findViewById(R.id.tvStatus);

checkStatus = findViewById(R.id.btnCheckStatus);

 Finds UI elements from activity_main.xml.

java

CopyEdit

checkStatus.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

 Sets a click listener for the button.

java

CopyEdit

checkAirPlaneModeStatus();

 Calls the method to check airplane mode.

java

CopyEdit

airplaneModeReceiver = new AirplaneModeReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);


registerReceiver(airplaneModeReceiver, filter);

 Registers the receiver to listen for airplane mode changes.

Checking Airplane Mode Status

java

CopyEdit

public void checkAirPlaneModeStatus(){

boolean isAirplaneModeOn =
Settings.Global.getInt(getContentResolver(),Settings.Global.AIRPLANE_MODE_ON,0)!=0;

String status = isAirplaneModeOn?"Airplane Mode is On":"Airplane Mode is Off";

statusText.setText(status);

Toast.makeText(this, ""+status, Toast.LENGTH_SHORT).show();

 Retrieves Airplane Mode status from system settings.

 Updates the TextView and displays a toast message.

Unregistering the Receiver

java

CopyEdit

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(airplaneModeReceiver);

 Stops listening for changes when the app is closed.


4. AndroidManifest.xml

This registers the receiver.

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>

 Registers AirplaneModeReceiver to receive Airplane Mode changes.

How the App Works

1. User opens the app.

2. User clicks "Check Status":

o It checks the current Airplane Mode.

o It registers the receiver.

3. If Airplane Mode changes:

o AirplaneModeReceiver detects the change.

o It shows a toast message.

4. When the app closes, the receiver is unregistered.

Final Thoughts
✔ Detects Airplane Mode changes in real-time.
✔ Displays the current Airplane Mode status.
✔ Registers and unregisters the receiver properly.

Would you like me to improve anything or add features? 🚀

4o

You said:

airplaneModeReceiver = new AirplaneModeReceiver(); IntentFilter filter = new


IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeReceiver, filter); //explain in detail

ChatGPT said:

Certainly! Let's break down this code line by line to understand what exactly is happening:

Code:

java

CopyEdit

airplaneModeReceiver = new AirplaneModeReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);

registerReceiver(airplaneModeReceiver, filter);

Step 1: Creating an Instance of AirplaneModeReceiver

java

CopyEdit

airplaneModeReceiver = new AirplaneModeReceiver();

What happens here?

 AirplaneModeReceiver is a class that extends BroadcastReceiver.

 new AirplaneModeReceiver() creates an object of this class.


 This object will listen for system broadcast messages (like Airplane Mode changes).

Step 2: Creating an IntentFilter

java

CopyEdit

IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);

What is IntentFilter?

 An IntentFilter tells Android which broadcasts this receiver should listen for.

 Intent.ACTION_AIRPLANE_MODE_CHANGED is a predefined system event triggered


whenever the user enables or disables Airplane Mode.

Breaking it down further:

 IntentFilter filter = new IntentFilter(...)

o Creates an IntentFilter object.

 Intent.ACTION_AIRPLANE_MODE_CHANGED

o This is a constant in the Intent class.

o It represents the Airplane Mode toggle event.

o Whenever Airplane Mode is turned ON or OFF, this event is sent system-wide.

Step 3: Registering the BroadcastReceiver

java

CopyEdit

registerReceiver(airplaneModeReceiver, filter);

What does registerReceiver() do?

 This method registers the airplaneModeReceiver object to start listening for


broadcasts.
 It uses the filter to specify which type of broadcast it should receive.

Breaking it down:

 registerReceiver(airplaneModeReceiver, filter);

o First Parameter (airplaneModeReceiver) → The BroadcastReceiver instance that


will handle the event.

o Second Parameter (filter) → Defines which broadcast actions the receiver should
listen for.

What happens after registration?

1. User toggles Airplane Mode ON or OFF.

2. The system sends a broadcast with the Intent.ACTION_AIRPLANE_MODE_CHANGED


action.

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.

What Happens If You Don't Register the Receiver?

If we don't call registerReceiver(airplaneModeReceiver, filter);, then:

 The app won't receive any Airplane Mode change notifications.

 Even if the AirplaneModeReceiver class exists, it won't do anything until registered.

Important Notes

1. Dynamic vs. Manifest-Declared Broadcast Receivers

o This is a dynamic BroadcastReceiver because we register it in code


(registerReceiver).

o In AndroidManifest.xml, we also have a manifest-declared receiver:

xml
CopyEdit

<receiver android:name=".AirplaneModeReceiver">

<intent-filter>

<action android:name="android.intent.action.AIRPLANE_MODE_CHANGED"/>

</intent-filter>

</receiver>

o The difference:

 Manifest-Declared Receivers → Always active, even when the app is not


running.

 Dynamic Receivers (like this one) → Active only when registered in code.

2. Must Unregister the Receiver

o If a receiver is registered dynamically, it must be unregistered in onDestroy():

java

CopyEdit

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(airplaneModeReceiver);

o Otherwise, the app may crash due to a memory leak.

Summary

 Creates an instance of AirplaneModeReceiver.

 Defines the IntentFilter to listen for AIRPLANE_MODE_CHANGED.

 Registers the receiver to start listening for changes.


 Triggers onReceive() in AirplaneModeReceiver when Airplane Mode is toggled.

 Shows a Toast message based on the new state.

You might also like