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

Ex 21

The document outlines an Android program that demonstrates system broadcast messages related to battery level and airplane mode status. It includes XML layout for the user interface and Java code for the MainActivity, which registers broadcast receivers for battery changes and airplane mode toggling. The program updates the UI to reflect the current battery level and airplane mode status when changes occur.

Uploaded by

Omkar Mankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Ex 21

The document outlines an Android program that demonstrates system broadcast messages related to battery level and airplane mode status. It includes XML layout for the user interface and Java code for the MainActivity, which registers broadcast receivers for battery changes and airplane mode toggling. The program updates the UI to reflect the current battery level and airplane mode status when changes occur.

Uploaded by

Omkar Mankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

X.

Exercise
1. Write a program to demonstrate all the system broadcast messages.

activity_main.xml
android:layout_gravity="left"
<?xml version="1.0" encoding="utf-8"?> android:text=" Battery level: " />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/r
<ProgressBar
es/android"
android:id="@+id/progressBar"
xmlns:app="http://schemas.android.com/apk/res-
auto" style="?android:attr/progressBarStyleHorizontal"
xmlns:tools="http://schemas.android.com/tools" android:layout_marginTop="8dp"
android:layout_width="match_parent" android:layout_marginLeft="12dp"
android:layout_height="match_parent" android:layout_marginRight="12dp"
android:gravity="center|top" android:max="100"
android:orientation="vertical" android:layout_width="match_parent"
tools:context=".MainActivity"> android:layout_height="wrap_content" />

<TextView <Space
android:id="@+id/txt" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:text="Broadcast Receiver" <TextView
android:textColor="#0C0C0C" android:id="@+id/AirMode"
android:textSize="20sp" /> android:layout_width="match_parent"
android:layout_height="wrap_content"
<Space android:text=" Turn on/off Airplane mode to
see status" />
android:layout_width="match_parent"
android:layout_height="21dp" />
</LinearLayout>

<TextView
android:id="@+id/battery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
MainActivity.java mReceiver = new
BatteryBroadcastReceiver();
package com.example.ex2101;
aReceiver = new
AirplaneModeChangeReceiver();
import
androidx.appcompat.app.AppCompatActivity;
AirplaneModeChangeReceiver aReceiver =
import android.content.BroadcastReceiver;
new AirplaneModeChangeReceiver();
import android.content.Context;
registerReceiver(aReceiver, new
import android.content.Intent; IntentFilter(Intent.ACTION_AIRPLANE_MODE
_CHANGED));
import android.content.IntentFilter;
import android.os.Bundle;
BatteryBroadcastReceiver mReceiver = new
import android.provider.Settings; BatteryBroadcastReceiver();
import android.widget.ProgressBar; registerReceiver(mReceiver, new
import android.widget.TextView; IntentFilter(Intent.ACTION_BATTERY_CHAN
GED));

public class MainActivity extends


AppCompatActivity { }

private TextView mBatteryLevelText, airmode; @Override

private ProgressBar mBatteryLevelProgress; protected void onStart() {

private BroadcastReceiver mReceiver, super.onStart();


aReceiver; registerReceiver(aReceiver,new
IntentFilter(Intent.ACTION_AIRPLANE_MODE
_CHANGED));
@Override
registerReceiver(mReceiver, new
protected void onCreate(Bundle IntentFilter(Intent.ACTION_BATTERY_CHAN
savedInstanceState) { GED));
super.onCreate(savedInstanceState); }
setContentView(R.layout.activity_main);

@Override
mBatteryLevelText = (TextView) protected void onStop() {
findViewById(R.id.battery);
super.onStop();
mBatteryLevelProgress = (ProgressBar)
findViewById(R.id.progressBar); unregisterReceiver(aReceiver);

airmode = (TextView) unregisterReceiver(mReceiver);


findViewById(R.id.AirMode); }
private class BatteryBroadcastReceiver extends BroadcastReceiver {
private final static String BATTERY_LEVEL = "level";

@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BATTERY_LEVEL, 0);

mBatteryLevelText.setText(" Battery level: " + level);


mBatteryLevelProgress.setProgress(level);
}
}

private class AirplaneModeChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (isAirplaneModeOn(context.getApplicationContext())) {
airmode.setText(" Airplane mode: ON");
} else {
airmode.setText(" Airplane mode: OFF");
}
}

private boolean isAirplaneModeOn(Context context) {


return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
}
Output:

You might also like