Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Mad 21

Mainactivity.java
package com.example.practice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private ListView broadcastListView;


private ArrayAdapter<String> broadcastAdapter;
private ArrayList<String> broadcastList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

broadcastList = new ArrayList<>();


broadcastAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, broadcastList);
broadcastListView = findViewById(R.id.broadcastListView);
broadcastListView.setAdapter(broadcastAdapter);

// Register the BroadcastReceiver


IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_BATTERY_LOW);
registerReceiver(systemBroadcastReceiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
// Unregister the BroadcastReceiver
unregisterReceiver(systemBroadcastReceiver);
}

private final BroadcastReceiver systemBroadcastReceiver = new


BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
String message = "Received system broadcast: " + action;
broadcastList.add(0, message); // Add message at the top
broadcastAdapter.notifyDataSetChanged(); // Notify ListView
adapter
Log.d("BroadcastReceiver", "Received broadcast: " +
action);
}
}
};
}

activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="System Broadcast Messages"
android:gravity="center"
android:textSize="18sp"
android:padding="16dp"
android:textStyle="bold"/>

<ListView
android:id="@+id/broadcastListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/titleTextView"
android:padding="16dp" />

</RelativeLayout>

Broadcastreciver class
package com.example.practice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SystemBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
showToast(context, "Boot completed");
break;
case Intent.ACTION_POWER_CONNECTED:
showToast(context, "Power connected");
break;
case Intent.ACTION_POWER_DISCONNECTED:
showToast(context, "Power disconnected");
break;
case Intent.ACTION_BATTERY_LOW:
showToast(context, "Battery low");
break;
default:
showToast(context, "Received system broadcast: " +
action);
break;
}
}
}

private void showToast(Context context, String message) {


Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}

You might also like