Practical-21
Develop a Program to Implement Broadcast Receiver
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="System_Broadcast!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MyBroadcastReceiver.java
package com.example.pract_21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean state = intent.getBooleanExtra("state", false);
Toast.makeText(context, "Airplane Mode: " + (state ? "ON" : "OFF"),
Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_BATTERY_CHANGED:
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Toast.makeText(context, "Battery Level: " + level + "%",
Toast.LENGTH_SHORT).show();
break;
case ConnectivityManager.CONNECTIVITY_ACTION:
Toast.makeText(context, "Network Status Changed",
Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_BOOT_COMPLETED:
Toast.makeText(context, "Device Boot Completed",
Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_POWER_CONNECTED:
Toast.makeText(context, "Power Connected", Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_POWER_DISCONNECTED:
Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_SCREEN_ON:
Toast.makeText(context, "Screen ON", Toast.LENGTH_SHORT).show();
break;
case Intent.ACTION_SCREEN_OFF:
Toast.makeText(context, "Screen OFF", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Pract21"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.pract_21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private MyBroadcastReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an instance of the receiver
myReceiver = new MyBroadcastReceiver();
// Register receiver dynamically
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(myReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver); // Unregister to avoid memory leaks
}
}