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

Green University of Bangladesh

Department of Computer Science and Engineering


(CSE)
Faculty of Sciences and Engineering
Semester: Fall, Year: 2022, B.Sc. in CSE

LAB REPORT NO #5
Course Title: Mobile Application Development
Course Code: CSE 436 Section: 211D3

Student Details

Name ID
Al Rayhan 201902186

Lab Date :
Submission Date :
Course Teacher’s Name :Sudip Chandra Ghosal

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:..............................................
Date:..............................
Title: Report on Battery Percentage Monitoring using
BroadcastReceiver in Android

1. Introduction

Monitoring battery percentage is crucial for mobile applications that require


optimized power usage. This project aims to create an Android application
that utilizes a BroadcastReceiver to detect changes in battery percentage and
display the updated percentage using a Toast message.

2. Objective

The primary objective of this project is to design and implement a


BroadcastReceiver in an Android application that:

- Listens for changes in the device's battery percentage.

- Displays a Toast message whenever the battery percentage changes.

3. Procedure

The project can be broken down into the following steps:

Step 1: Setting Up the Project

1. Create a new Android project in Android Studio.


2. Define the necessary project structure including the main activity and
resources.

Step 2: Declaring the BroadcastReceiver in Manifest

1. Open `AndroidManifest.xml`.

2. Declare the BroadcastReceiver and specify the `BATTERY_CHANGED`


action.

Step 3: Implementing the BroadcastReceiver

1. Create a Java class named `BatteryBroadcastReceiver` extending


`BroadcastReceiver`.

2. Override the `onReceive` method to handle battery percentage changes and


display a Toast message.

Step 4: Optional MainActivity Implementation

1. Optionally, set up dynamic registration and unregistration of the


BroadcastReceiver in `MainActivity`.

4. Design

The design consists of two main components:

-BroadcastReceiver: A receiver to handle battery change events.


MainActivity: An optional activity for dynamic registration and to provide a
user interface.

5. Implementation

The implementation details are divided into XML and Java code.

BatteryBroadcastReceiver.java

package com.example.batteryexample;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.BatteryManager;

import android.widget.TextView;

public class BatteryLevelReceiver extends BroadcastReceiver {

private TextView batteryLevelTextView;

public BatteryLevelReceiver(TextView textView) {


this.batteryLevelTextView = textView;

@Override

public void onReceive(Context context, Intent intent) {

int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float) scale * 100;

batteryLevelTextView.setText("Battery Level: " + batteryPct + "%");

public static IntentFilter getBatteryLevelIntentFilter() {

return new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

MainActivity.java (Optional)

package com.example.batteryexample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.IntentFilter;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private BatteryLevelReceiver batteryLevelReceiver;

private TextView batteryLevelTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

batteryLevelTextView = findViewById(R.id.batteryLevelTextView);

batteryLevelReceiver = new
BatteryLevelReceiver(batteryLevelTextView);

@Override

protected void onStart() {

super.onStart();

registerReceiver(batteryLevelReceiver,
BatteryLevelReceiver.getBatteryLevelIntentFilter());
}

@Override

protected void onStop() {

super.onStop();

unregisterReceiver(batteryLevelReceiver);

activity_main.xml (Optional)

<?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/batteryLevelTextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Battery Level: "

android:textSize="24sp"

android:layout_centerInParent="true" />

</RelativeLayout>

6. Output

When the application is run, and the battery percentage changes, the
`BatteryBroadcastReceiver` will be triggered. A Toast message will appear on
the screen displaying the updated battery percentage.
7. Conclusion

This project successfully demonstrates how to use a BroadcastReceiver to


monitor battery percentage changes in an Android application. The
implementation ensures that the application can respond to battery changes
and provide feedback to the user, which is essential for applications that
need to manage power consumption effectively. The dynamic registration
approach also illustrates how to manage the receiver lifecycle within an
activity, ensuring efficient resource use and avoiding memory leaks.

You might also like