Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Display Battery Status using Broadcast Receiver in Android

This is simple example of Android Broadcast Receiver. In this android project, we will learn how to use broadcast receiver and how to get the Battery level and display it on the progress bar. We used one text view to display battery status and one progress bar to display battery level in progress. Create a new project, Open an XML file and use text view and progress bar in a linear layout. The code of the Android XML file is given below:

Display Battery level using Broadcast Receiver in Android
Check Battery level using Broadcast Receiver

<LinearLayout xmlns:android=""
android:background="#abc" >
android:minWidth="200dp" />

Now open your Java file, initialize broadcast object and use broadcast class as a nested class. Here, we are registering a broadcast object with a battery changed intent filter which will receive updates on every level of the battery changed. Whenever the battery level changes, onReceive() method will be called, which will take two parameters. Use the code in onReceive() method to do whatever is required to be done on every level of battery change. We are setting a level on progress bar, displaying level on text view and whenever battery level becomes 100% then a song, stored in the assets folder, will start playing. The code of the Android Java file is given below with explanation:

package com.exampl.aaaa; //your package name
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
//broadcast class is used as a nested class
private BroadcastReceiver mbcr=new BroadcastReceiver()
//onReceive method will receive updates
public void onReceive(Context c, Intent i)
//initially level has 0 value
//after getting update from broadcast receiver
//it will change and give battery status
int level=i.getIntExtra("level", 0);
//initialize all objects
ProgressBar pb=(ProgressBar)findViewById(;
TextView tv=(TextView)findViewById(;
//set level of progress bar
//display level on text view
//start a song when the battery level touches 100%
//Save small.mp4 in assets folder
//we can not start a media file from the drawable folder directly in broadcast method
//hence we used the assets folder
AssetFileDescriptor afd=getAssets().openFd("small.mp4");
MediaPlayer mp=new MediaPlayer();
//set file and starting point and ending point in bytes
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
//start song
catch(IOException e){}
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
//set layout
//register broadcast receiver
//Get battery changed status
//we can get more options like power connect, disconnect, call, etc.
//To get more options, write Intent followed by a dot(.) and press CTRL+Space
//you will get all the options
registerReceiver(mbcr,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

Now run your project and test application. If you have any doubts please comment. Share and help others.

Related Tutorials:-

★ Convert Text to Speech

★ Use Shared Preferences

★ Perform action on any Hardware button

★ Get all Mobile Contacts

★ Display Time using Broadcast Receiver

This post first appeared on Coders Hub: Android Code Examples And Programming Tutorials, please read the originial post: here

Share the post

Display Battery Status using Broadcast Receiver in Android


Subscribe to Coders Hub: Android Code Examples And Programming Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription