0% found this document useful (0 votes)
15 views2 pages

Android System Broadcast Receiver

The document describes an Android application that uses a BroadcastReceiver to listen for various system broadcasts such as boot completion, battery status, power connection, and airplane mode changes. The MainActivity class updates a TextView with messages corresponding to these broadcasts. The layout is defined in an XML file, which includes a TextView to display the current system status.

Uploaded by

omyenpure28
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)
15 views2 pages

Android System Broadcast Receiver

The document describes an Android application that uses a BroadcastReceiver to listen for various system broadcasts such as boot completion, battery status, power connection, and airplane mode changes. The MainActivity class updates a TextView with messages corresponding to these broadcasts. The layout is defined in an XML file, which includes a TextView to display the current system status.

Uploaded by

omyenpure28
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/ 2

Android System Broadcast Receiver

Java File: MainActivity.java

// MainActivity.java
package com.example.systembroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView textView;


private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
textView.setText("System Boot Completed");
break;
case Intent.ACTION_BATTERY_LOW:
textView.setText("Battery Low");
break;
case Intent.ACTION_POWER_CONNECTED:
textView.setText("Power Connected");
break;
case Intent.ACTION_POWER_DISCONNECTED:
textView.setText("Power Disconnected");
break;
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean state = intent.getBooleanExtra("state", false);
textView.setText("Airplane Mode: " + (state ? "Enabled" :
"Disabled"));
break;
}
}
}
};

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

IntentFilter filter = new IntentFilter();


filter.addAction(Intent.ACTION_BOOT_COMPLETED);
filter.addAction(Intent.ACTION_BATTERY_LOW);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);

registerReceiver(broadcastReceiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
}

XML File: activity_main.xml

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for System Broadcast..."
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>

You might also like