0% found this document useful (0 votes)
101 views3 pages

Airplane Mode Notification App Guide

The document outlines the creation of an Android application that notifies users when Airplane mode is toggled using a broadcast receiver. It includes code snippets for the MainActivity, AirplaneModeChangeReceiver, and the layout file activity_main.xml. The application successfully implements the desired functionality and concludes with a confirmation of its successful creation.

Uploaded by

shaikhtas15
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)
101 views3 pages

Airplane Mode Notification App Guide

The document outlines the creation of an Android application that notifies users when Airplane mode is toggled using a broadcast receiver. It includes code snippets for the MainActivity, AirplaneModeChangeReceiver, and the layout file activity_main.xml. The application successfully implements the desired functionality and concludes with a confirmation of its successful creation.

Uploaded by

shaikhtas15
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

Practical 3.

a 15th March, 2025

Notifications
Aim: To Create an android application which automatically notify the user when Aeroplane mode is turned
on or off using broadcast receiver.

[Link] - Location: app/src/main/java/com/example/practical3a/[Link]

package [Link].practical3a

import [Link] import


[Link] import [Link]
import [Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link]
import [Link]

class MainActivity : ComponentActivity() { lateinit


var receiver: AirplaneModeChangeReceiver override
fun onCreate(savedInstanceState: Bundle?)
{ [Link](savedInstanceState)
setContentView([Link].activity_main)

receiver=AirplaneModeChangeReceiver()
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED).also {
registerReceiver(receiver,it)
}
}

override fun onStop()


{ [Link]()
unregisterReceiver(receiver)
}}

Images ([Link]) - Location: app/src/main/res/drawable/

Download product images → Place them inside the res > drawable folder in your Android project.

Rename them appropriately (e.g. [Link].).


[Link] - Location: app/src/main/java/com/example/practical3a/
[Link]

package [Link].practical3a

import [Link]
import [Link] import
[Link]
import [Link]

class AirplaneModeChangeReceiver : BroadcastReceiver()


{ override fun onReceive(context: Context?, intent: Intent?) {
val isAirplaneModeEnabled = intent?.getBooleanExtra("state",false)?:return

if (isAirplaneModeEnabled){
[Link](context,"Airplane Mode Enabled",Toast.LENGTH_LONG).show()
}
else
{
[Link](context,"Airplane Mode Disabled",Toast.LENGTH_LONG).show()
}
}}

activity_main.xml - Location: app/src/main/res/layout/activity_main.xml

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


<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broadcasting System"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center"
android:paddingBottom="16dp"/>

<ImageView
android:layout_width="237dp"
android:layout_height="263dp"
android:contentDescription="Airplane Image"
android:src="@drawable/airplane" />
</LinearLayout>

Output:

Conclusion: The application which automatically notifies the user when Aeroplane mode is turned on or off
using broadcast receiver was created Successfully.

Common questions

Powered by AI

The implementation choice of using a LinearLayout in activity_main.xml is effective for this application's user interface because it provides a straightforward and predictable way to arrange UI components vertically. By specifying the 'vertical' orientation and a 'center' gravity, the TextView and ImageView are neatly aligned in the center, offering a clean and organized layout. This choice simplifies layout design and ensures compatibility across different screen sizes, aligning with best practices for responsive UI design in Android applications .

Using the getBooleanExtra method in the AirplaneModeChangeReceiver class is important for extracting the airplane mode status from the Intent passed to the onReceive method. This method retrieves the 'state' extra from the intent, which is a boolean indicating whether airplane mode is enabled (true) or disabled (false). Accurately extracting this information is crucial as it determines the message shown to the user through the Toast notification .

The use of Toast messages is appropriate for notifying users about changes in airplane mode status as they provide transient feedback in a lightweight manner without interrupting user activity or requiring additional user interaction. Toasts are ideal for non-critical notifications like status updates, ensuring users are aware of mode changes while maintaining a seamless user experience .

The AirplaneModeChangeReceiver class acts as a BroadcastReceiver that listens for changes in the airplane mode status of the device. It overrides the onReceive function, which gets triggered whenever the airplane mode state changes. The intent passed to this function contains an extra 'state' that indicates whether airplane mode is enabled or disabled. Based on this, the receiver displays a Toast message to the user, informing them about the current airplane mode status .

The setContentView method in MainActivity.kt is used to set the user interface layout for the activity. It inflates the layout defined in activity_main.xml and renders it on the screen when the activity starts. By calling this method, the UI framework knows which layout components to display and how to position them, enabling users to interact with the elements defined in the XML file .

Unregistering the broadcast receiver in the onStop method of the MainActivity class is crucial to prevent the application from consuming unnecessary resources and to avoid memory leaks. If the receiver remains registered while the activity is no longer active or visible, it can still respond to system broadcasts, leading to unintended behavior and resource wastage. This practice also aligns with good lifecycle management in Android applications and ensures efficient usage of system resources .

The Intent.ACTION_AIRPLANE_MODE_CHANGED filter is significant as it specifies the type of broadcast intents that the AirplaneModeChangeReceiver should listen for. This filter informs the Android system to notify the receiver whenever there is a change in the airplane mode status of the device, allowing the application to react accordingly by notifying the user through a Toast message .

In MainActivity.kt, the lifecycle of the broadcast receiver is managed by registering the AirplaneModeChangeReceiver in the onCreate method, which is called when the activity is created. The receiver is registered with an IntentFilter that listens for the 'ACTION_AIRPLANE_MODE_CHANGED' action. The receiver is unregistered in the onStop method to ensure it does not continue to listen for broadcast intents when the activity is no longer visible, preventing leaks or unnecessary overhead .

The application ensures the correct display of images relevant to airplane mode by placing the image files inside the 'res > drawable' folder and referencing them in the XML layout file. In this case, an ImageView in activity_main.xml is set to display an image with the source '@drawable/airplane', ensuring the correct airplane mode-related image is shown in the interface .

Failing to handle null checks in the AirplaneModeChangeReceiver's onReceive method can lead to application crashes. If the Intent or its extras are null and the null checks are not in place, attempting to access them could throw a NullPointerException, disrupting the user experience and potentially causing the application to terminate unexpectedly. Implementing null checks, as seen in the method, ensures robust error handling and application stability .

You might also like