Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Get Battery Health Info in Android

Viewed:  2256 
Posted On:  24/12/2012 21:46:05 
How to get Android Mobile Battery information like charging status, Charging type, Product name, company name... 

In this article I am going to explain how we can know information about our android mobile battery.  With the help of this article we can know what is the current status of our battery mean how much percentage of charge battery, What is the condition of our battery, what is the company name, right now it's charging or not, what is the mode of charging AC/DC.

Let me explain the application:

Create  a New Android Project with the name of GetBatteryHealthInfo -> Now open res -> layout -> activity_main.xml

The code will be look like below here...

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >
    android:text="Dear Rahul Your Battery Info:"/>

Graphical Representation will be:


Image 1.

Now the turn to the code from where we can get the battery related info and display it to the UI.

So open src -> com.example.getbatteryhealthinfo ->

package com.example.getbatteryhealthinfo;

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.util.Log;
import android.widget.TextView;
import android.view.Menu;

 class MainActivity extends Activity {
       TextView textBatteryLevel = null;
    String batteryLevelInfo = "Battery Level";
       protected void onCreate(Bundle savedInstanceState) {
              textBatteryLevel = (TextView)findViewById(;

          //This is the method which will get All info of Battery

           protected void onDestroy()

           private BroadcastReceiver battery_receiver = new BroadcastReceiver()
                   public void onReceive(Context context, Intent intent)
                           boolean isPresent = intent.getBooleanExtra("present"false);
                           String technology = intent.getStringExtra("technology");
                           int plugged = intent.getIntExtra("plugged", -1);
                           int scale = intent.getIntExtra("scale", -1);
                           int health = intent.getIntExtra("health", 0);
                           int status = intent.getIntExtra("status", 0);
                           int rawlevel = intent.getIntExtra("level", -1);
               int level = 0;        

               Bundle bundle = intent.getExtras();                            

                       if (rawlevel >= 0 && scale > 0) {
                           level = (rawlevel * 100) / scale;

                       String batteryinfo = "Battery Charging Level: " + level + "%\n";
                       batteryinfo += ("Technology: " + technology + "\n");
                       batteryinfo += ("Plugged: " + getPlugTypeString(plugged) + "\n");
                       batteryinfo += ("Health: " + getHealthString(health) + "\n");
                       batteryinfo += ("Status: " + getStatusString(status) + "\n");
                       setBatteryLevelText(batteryinfo "\n\n" + bundle.toString());
                   setBatteryLevelText("Battery not present!!!");

           private String getPlugTypeString(int plugged){
                   String plugType = "Unknown";
                           case BatteryManager.BATTERY_PLUGGED_AC: plugType = "AC";        break;
                           case BatteryManager.BATTERY_PLUGGED_USB: plugType = "USB";      break;

                   return plugType;

           private String getHealthString(int health)
                   String healthString = "Unknown";
                           case BatteryManager.BATTERY_HEALTH_DEAD: healthString = "Dead"break;
                           case BatteryManager.BATTERY_HEALTH_GOOD: healthString = "Good"break;
                           case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: healthString = "Over Voltage"break;
                           case BatteryManager.BATTERY_HEALTH_OVERHEAT: healthString = "Over Heat"break;
                           case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthString = "Failure"break;
                   return healthString;

           private String getStatusString(int status)
                   String statusString = "Unknown";
                           case BatteryManager.BATTERY_STATUS_CHARGING: statusString = "Charging"break;
                           case BatteryManager.BATTERY_STATUS_DISCHARGING: statusString = "Discharging"break;
                           case BatteryManager.BATTERY_STATUS_FULL: statusString = "Full"break;
                           case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusString = "Not Charging"break;
                   return statusString;

           private void setBatteryLevelText(String text){
           private void registerBatteryLevelReceiver(){
                   IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
           registerReceiver(battery_receiver, filter);

       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(, menu);
              return true;

When we run this app with simulator the UI will be:


Image 2.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us