Ex 21
Ex 21
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));
@Override
mBatteryLevelText = (TextView) protected void onStop() {
findViewById(R.id.battery);
super.onStop();
mBatteryLevelProgress = (ProgressBar)
findViewById(R.id.progressBar); unregisterReceiver(aReceiver);
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BATTERY_LEVEL, 0);
@Override
public void onReceive(Context context, Intent intent) {
if (isAirplaneModeOn(context.getApplicationContext())) {
airmode.setText(" Airplane mode: ON");
} else {
airmode.setText(" Airplane mode: OFF");
}
}