0% found this document useful (0 votes)
4 views4 pages

Practical No. 21

The document provides a code example for an Android application that demonstrates how to receive and display system broadcast messages using a BroadcastReceiver. It includes the layout XML for the main activity, the Java code for handling various system broadcasts such as battery changes and screen on/off events, and the necessary permissions and receiver declarations in the AndroidManifest.xml file. The application updates a TextView with the latest broadcast message received from the system.

Uploaded by

ganeshkumbhar638
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)
4 views4 pages

Practical No. 21

The document provides a code example for an Android application that demonstrates how to receive and display system broadcast messages using a BroadcastReceiver. It includes the layout XML for the main activity, the Java code for handling various system broadcasts such as battery changes and screen on/off events, and the necessary permissions and receiver declarations in the AndroidManifest.xml file. The application updates a TextView with the latest broadcast message received from the system.

Uploaded by

ganeshkumbhar638
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/ 4

Q.

Write a program to demonstrate all the private TextView broadcastTextView;


system broadcast messages.
@Override
activity_main.java
protected void onCreate(Bundle
<?xml version="1.0" encoding="utf-8"?> savedInstanceState) {
<LinearLayout super.onCreate(savedInstanceState);
xmlns:android="http://schemas.android.com/apk/r setContentView(R.layout.activity_main);
es/android"
android:layout_width="match_parent" // Initialize TextView to display broadcast
android:layout_height="match_parent" messages
android:orientation="vertical" broadcastTextView =
android:background="@color/white" findViewById(R.id.broadcastTextView);
android:gravity="center"
android:padding="16dp"> // Register a dynamic BroadcastReceiver for
<TextView system events
android:id="@+id/broadcastTextView" IntentFilter filter = new IntentFilter();
android:layout_width="match_parent"
android:layout_height="wrap_content" filter.addAction(Intent.ACTION_AIRPLANE_MO
android:text="Waiting for System DE_CHANGED);
Broadcasts..."
android:gravity="center" filter.addAction(Intent.ACTION_BATTERY_CHA
android:textColor="@color/black" NGED);
android:textSize="25sp" />
</LinearLayout> filter.addAction(Intent.ACTION_POWER_CONN
ECTED);
MainActivity.java
filter.addAction(Intent.ACTION_POWER_DISC
package com.example.practicalno21; ONNECTED);

import android.content.BroadcastReceiver; filter.addAction(Intent.ACTION_SCREEN_ON);


import android.content.Context;
import android.content.Intent; filter.addAction(Intent.ACTION_SCREEN_OFF);
import android.content.IntentFilter;
import android.os.BatteryManager; filter.addAction(Intent.ACTION_USER_PRESEN
import android.os.Bundle; T);
import android.widget.TextView;
import registerReceiver(new
androidx.appcompat.app.AppCompatActivity; SystemBroadcastReceiver(), filter);
}
public class MainActivity extends
AppCompatActivity { // Inner class to handle system-wide broadcasts
dynamically
private class SystemBroadcastReceiver extends break;
BroadcastReceiver {
@Override case Intent.ACTION_USER_PRESENT:
public void onReceive(Context context, message = "User Present";
Intent intent) { break;
String action = intent.getAction();
String message = ""; default:
message = "Unknown Action
switch (action) { Received";
case }
Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean isAirplaneModeOn = // Update the TextView with the broadcast
intent.getBooleanExtra("state", false); message
message = "Airplane Mode Changed: broadcastTextView.setText(message);
" + (isAirplaneModeOn ? "ON" : "OFF"); }
break; }

case // Static inner class to handle


Intent.ACTION_BATTERY_CHANGED: BOOT_COMPLETED broadcast
int level = public static class BootCompletedReceiver
intent.getIntExtra(BatteryManager.EXTRA_LEVE extends BroadcastReceiver {
L, -1); @Override
message = "Battery Level: " + level + public void onReceive(Context context,
"%"; Intent intent) {
break; if
(Intent.ACTION_BOOT_COMPLETED.equals(int
case ent.getAction())) {
Intent.ACTION_POWER_CONNECTED: // Start MainActivity after boot
message = "Power Connected"; completed
break; Intent startIntent = new Intent(context,
MainActivity.class);
case
Intent.ACTION_POWER_DISCONNECTED: startIntent.addFlags(Intent.FLAG_ACTIVITY_NE
message = "Power Disconnected"; W_TASK);
break; context.startActivity(startIntent);
}
case Intent.ACTION_SCREEN_ON: }
message = "Screen Turned ON"; }
break; }

case Intent.ACTION_SCREEN_OFF: AndroidManifest.xml


message = "Screen Turned OFF";
<?xml version="1.0" encoding="utf-8"?> </activity>
<manifest
xmlns:android="http://schemas.android.com/apk/r <!-- Receiver for BOOT_COMPLETED -->
es/android" <receiver

xmlns:tools="http://schemas.android.com/tools"> android:name=".MainActivity$BootCompletedRe
ceiver"
<!-- Permission to receive android:enabled="true"
BOOT_COMPLETED broadcast --> android:exported="true">
<uses-permission <intent-filter>
android:name="android.permission.RECEIVE_B <action
OOT_COMPLETED" /> android:name="android.intent.action.BOOT_CO
MPLETED" />
<application <category
android:allowBackup="true" android:name="android.intent.category.DEFAUL
T" />
android:dataExtractionRules="@xml/data_extract </intent-filter>
ion_rules" </receiver>
</application>
android:fullBackupContent="@xml/backup_rules
" </manifest>
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_roun
d"
android:supportsRtl="true"

android:theme="@style/Theme.PracticalNo21"
tools:targetApi="31">

<!-- Main Activity -->


<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCH
ER" />
</intent-filter>

You might also like