Display battery information of android device

Posted By : Ravi Sharma | 13-Aug-2013

Android's BatteryManager class provides the battery information like status, health..., which we will be making use of to obtain battery information of our android device.

The xml file is as follows-
 


	<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"

	    android:paddingBottom="@dimen/activity_vertical_margin"

	    android:paddingLeft="@dimen/activity_horizontal_margin"

	    android:paddingRight="@dimen/activity_horizontal_margin"

	    android:paddingTop="@dimen/activity_vertical_margin"

	    tools:context=".BatteryActivity"

	    android:background="@drawable/images">

	

	    <ScrollView

	        android:id="@+id/scrollView1"

	        android:layout_width="wrap_content"

	        android:layout_height="wrap_content" >

	

	        <TextView

	            android:id="@+id/text"

	            android:layout_width="wrap_content"

	            android:layout_height="wrap_content"

	            android:text="Battery Info"

	            android:textColor="#ffffff"

	            android:textSize="25sp" />

	    </ScrollView>

	

	    <ProgressBar

	        android:id="@+id/progressBar"

	        style="?android:attr/progressBarStyleHorizontal"

	        android:layout_width="200sp"

	        android:layout_height="wrap_content"

	        

	        android:layout_marginLeft="34dp"

	        android:layout_toRightOf="@+id/scrollView1"

	         />

	

	</RelativeLayout>

We will be using a textview, a progress bar to show the battery level. Its fairly simple as you can see it ,nothing fancy here. Here is the code which is also very easy.

 


	package com.example.batteryinformation;

	

	import android.os.BatteryManager;

	import android.os.Bundle;

	import android.widget.ProgressBar;

	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 BatteryActivity extends Activity {

	int health;

	int icon_small;

	    int level;

	    int plugged;

	    boolean present;

	    int scale;

	    int status;

	    String technology;

	    int temperature;

	    int voltage;

	    TextView text;

	    ProgressBar pb;

	

	    @Override

	    protected void onCreate(Bundle savedInstanceState) {

	        super.onCreate(savedInstanceState);

	        setContentView(R.layout.activity_battery);

	        registerReceiver(mBatInfoReceiver, new IntentFilter(

	                Intent.ACTION_BATTERY_CHANGED));

	        text = (TextView) findViewById(R.id.text);

	        pb = (ProgressBar) findViewById(R.id.progressBar);

	

	    }

	

	    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {

	        @Override

	        public void onReceive(Context c, Intent intent) {

	            

	            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

	            health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);

	            icon_small = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);

	            plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

	            present = intent.getExtras().getBoolean(

	                    BatteryManager.EXTRA_PRESENT);

	            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);

	            status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);

	            technology = intent.getExtras().getString(

	                    BatteryManager.EXTRA_TECHNOLOGY);

	            temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,

	                    0);

	            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

	            

	            pb.setProgress(level);

	            

	            text.setText("Level: " + level + "\n\n" + "Health: " + health + "\n\n"

	                    + "Icon Small:" + icon_small + "\n\n" +

	

	                    "Plugged: " + plugged + "\n\n" + "Present: " + present + "\n\n"

	                    + "Scale: " + scale + "\n\n" + "Status: " + status + "\n\n"

	                    + "Technology: " + technology + "\n\n" + "Temperature: "

	                    + temperature + "\n\n" + "Voltage: " + voltage + "\n\n");

	

	        }

	    };

	}

In onCreate method we simply register the broadcast receiver that provides us the battery information.

In broadcast receiver's onReceive method we use intent to extract the required value by making use of BatteryManager's string .

Here we have used ten string values , you can easily make out what they mean by the name itself.


String     EXTRA_HEALTH               Extra for ACTION_BATTERY_CHANGED: integer containing the current health constant.
String     EXTRA_ICON_SMALL     Extra for ACTION_BATTERY_CHANGED: integer containing the resource ID of a small status bar icon indicating the                                                                  current battery state.
String     EXTRA_LEVEL                  Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery level, from 0 to EXTRA_SCALE.
String     EXTRA_PLUGGED           Extra for ACTION_BATTERY_CHANGED: integer indicating whether the device is plugged in to a power source; 0                                                                      means it is on battery, other constants are different types of power sources.
String     EXTRA_PRESENT           Extra for ACTION_BATTERY_CHANGED: boolean indicating whether a battery is present.
String     EXTRA_SCALE                 Extra for ACTION_BATTERY_CHANGED: integer containing the maximum battery level.
String     EXTRA_STATUS               Extra for ACTION_BATTERY_CHANGED: integer containing the current status constant.
String     EXTRA_TECHNOLOGY     Extra for ACTION_BATTERY_CHANGED: String describing the technology of the current battery.
String     EXTRA_TEMPERATURE    Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.
String     EXTRA_VOLTAGE             Extra for ACTION_BATTERY_CHANGED: integer containing the current battery voltage level.


That's it, here is the output from running our code.


Download Source Code

Thanks,

Ravi Sharma

[email protected]

 

About Author

Author Image
Ravi Sharma

Ravi Sharma is an Android application developer with experience in Java , Titanium and Phonegap frameworks. Ravi loves drawing and PC games.

Request for Proposal

Name is required

Comment is required

Sending message..