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

Get Battery Health Info in Android

Article:
Viewed:  2029 
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="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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dip"
    android:id="@+id/batterylevel_text"
    android:textStyle="bold"
    android:text="Dear Rahul Your Battery Info:"/>
 
</RelativeLayout>

Graphical Representation will be:

1.png

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 -> MainActivity.java

package com.example.getbatteryhealthinfo;

import
 android.app.Activity;
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;

public
 class MainActivity extends Activity {
       TextView textBatteryLevel = null;
    String batteryLevelInfo = "Battery Level";
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              textBatteryLevel = (TextView)findViewById(R.id.batterylevel_text);
           
           registerBatteryLevelReceiver();

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

        @Override
           protected void onDestroy()
           {
                   unregisterReceiver(battery_receiver);        
                   super.onDestroy();
           }        

           private BroadcastReceiver battery_receiver = new BroadcastReceiver()
           {
                   @Override
                   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(isPresent)
               {
                       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());
               }
               else
               {
                   setBatteryLevelText("Battery not present!!!");
               }
                   }
           };          

           private String getPlugTypeString(int plugged){
                   String plugType = "Unknown";
                   
                   switch(plugged)
                   {
                           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";
                   
                   switch(health)
                   {
                           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";
                   
                   switch(status)
                   {
                           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){
                   textBatteryLevel.setText(text);
           }
           
           private void registerBatteryLevelReceiver(){
                   IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                   
           registerReceiver(battery_receiver, filter);
           }
          

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

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

2.png

Image 2.

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