banner_battery
January 25, 2016

Android – Getting Battery Information

By admin


We have many apps on play store which gives battery information along with many other CPU informations such as CPU-Z, CPU X etc. In Android, one can easily obtain the battery information at any point in time using the BatteryManager class . It  contains several strings and constants used for values in the ACTION_BATTERY_CHANGED Intent. Android Developers can display these values using an appropriate Views in their application.

Using the Battery API one can get a battery’s level, voltage, temperature, technology, charging status, health and even an icon representing the current state of the battery. Through this post, we will learn how to display the battery information in Android App.

Step 1 ) Create xml in layout 

activity_battery.xml

<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">
   
    <TextView
        android:id="@+id/txtBatteryInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
         
</RelativeLayout>

Step 2 ) Create Activity and Register Broadcast Receiver .

To fetch battery information in android you need to create new activity class named BatteryInfoActivity in your existing android project. In order to receive the updated information from the battery we need to register a BroadcastReceiver and an IntentFilter.

BatteryInfoActivity.java

package com.android.batteryinfo;

import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BatteryInfoActivity extends Activity {

private TextView txtBatteryInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_battery);

      txtBatteryInfo = (TextView)findViewById(R.id.txtBatteryInfo);
      registerReceiver(this.batteryInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

}

BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
      int health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
      int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
      int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
      boolean present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
      int status= intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
      String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
      int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
      int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

      txtBatteryInfo.setText("Health: "+health+"\n"+
      "Level: "+level+"\n"+
      "Plugged: "+plugged+"\n"+
      "Present: "+present+"\n"+
      "Status: "+status+"\n"+
      "Technology: "+technology+"\n"+
      "Temperature: "+temperature+"\n"+
      "Voltage: "+voltage+"\n");

     }
   };
}

android_battery_demo

Enjoy Coding and Share Knowledge