0% found this document useful (0 votes)
6 views3 pages

PRCT 21

The document contains code for an Android application that sets up a broadcast receiver to listen for various system events such as boot completion, battery status changes, and screen on/off actions. It includes the main activity class and the broadcast receiver class, demonstrating how to register and unregister the receiver. The application logs received broadcasts and displays a toast message when an event occurs.

Uploaded by

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

PRCT 21

The document contains code for an Android application that sets up a broadcast receiver to listen for various system events such as boot completion, battery status changes, and screen on/off actions. It includes the main activity class and the broadcast receiver class, demonstrating how to register and unregister the receiver. The application logs received broadcasts and displays a toast message when an event occurs.

Uploaded by

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

Name : Adina Hawaldar Enrollnment No : 2205690267

<?xml version="1.0" encoding="utf-8"?> import


androidx.appcompat.app.AppCompatActivity;
<LinearLayout
xmlns:android="http://schemas.android.com/ public class MainActivity extends
apk/res/android" AppCompatActivity {

private SystemBroadcastReciever receiver;


xmlns:app="http://schemas.android.com/apk
/res-auto" @Override

protected void onCreate(Bundle


xmlns:tools="http://schemas.android.com/to savedInstanceState) {
ols" super.onCreate(savedInstanceState);
android:layout_width="match_parent" setContentView(R.layout.activity_main);
android:layout_height="match_parent" receiver = new
tools:context=".MainActivity"> SystemBroadcastReciever();

<TextView

android:layout_width="wrap_content" IntentFilter filter = new IntentFilter();

android:layout_height="wrap_content"
filter.addAction(Intent.ACTION_BOOT_COMPL
android:text="Hello World!" ETED);

/>
filter.addAction(Intent.ACTION_BATTERY_LO
</LinearLayout> W);

JAVA CODE: filter.addAction(Intent.ACTION_BATTERY_OKA


Y);

package com.adina.mad;
filter.addAction(Intent.ACTION_POWER_CON
import android.content.Intent; NECTED);

import android.content.IntentFilter;
filter.addAction(Intent.ACTION_POWER_DISC
import android.os.Bundle;
ONNECTED);
import android.util.Log;
SystemBroadcastReciever code:
filter.addAction(Intent.ACTION_SCREEN_ON);
package com.adina.mad;

filter.addAction(Intent.ACTION_SCREEN_OFF); import android.content.BroadcastReceiver;

import android.content.Context;
filter.addAction(Intent.ACTION_AIRPLANE_M import android.content.Intent;
ODE_CHANGED);
import android.util.Log;

filter.addAction(Intent.ACTION_USER_PRESEN import android.widget.Toast;


T);
public class SystemBroadcastReciever extends
BroadcastReceiver {
filter.addAction(Intent.ACTION_DATE_CHANG
@Override
ED);
public void onReceive(Context context,
Intent intent) {
filter.addAction(Intent.ACTION_TIME_CHANG
ED); if (intent != null && intent.getAction() !=
null) {
filter.addAction(Intent.ACTION_TIMEZONE_C String action = intent.getAction();
HANGED);
Log.d("Testing", "Received Broadcast: "
+ action);
filter.addAction(Intent.ACTION_SHUTDOWN);
Toast.makeText(context, "Broadcast
registerReceiver(receiver, filter); Received: " + action,
Toast.LENGTH_LONG).show();
Log.d("MainActivity", "Broadcast Receiver
Registered"); }
} }
@Override }
protected void onDestroy() {

super.onDestroy();

unregisterReceiver(receiver);

Log.d("MainActivity", "Broadcast Receiver


Unregistered");

You might also like