Sunday, August 2, 2015

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="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="#abc" >
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=""
  android:textSize="25sp"
  android:layout_gravity="center"
/>
<ProgressBar
  android:id="@+id/progressBar1"
  style="?android:attr/progressBarStyleHorizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:minHeight="100dp"
  android:minWidth="200dp" />
</LinearLayout>

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.smr; //your package name
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
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(R.id.progressBar1);
  TextView tv=(TextView)findViewById(R.id.textView1);
  //set level of progress bar
  pb.setProgress(level);
  //display level on text view
  tv.setText("Batterylevel:"+Integer.toString(level)+"%");
  //start a song when the battery level touches 100%
  if(level==100)
  {
   try
   {
   //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());
   mp.prepare();
   //start song
   mp.start();
    }
    catch(IOException e){}
   }
  }
};
  /** Called when the activity is first created. */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   //set layout
   setContentView(R.layout.activity_main);
   //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.

No comments:

Post a Comment