[Android ]Get Device Battery Information

Public on May 31, 2014

The battery API is quite impressive, between other things not only can you get a battery’s voltage but you can also get the temperature, status and even an icon representing the current state of the battery.
In this post we’ll make an app that display’s all of the device’s battery information in a text view whenever there is a change of state in the battery.

The App’s Interface

Our interface will consists of a simple text view where we will show the battery’s information and an image view where we will display the battery’s current state using the image provided by the API to do this.



File: layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

 

    <TextView

        android:id="@+id/textViewBatteryInfo"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Battery Info: " />

 

    <ImageView

        android:id="@+id/imageViewBatteryState"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/icon" />

 

</LinearLayout>


This is how the app will look.
Phone battery information
the result: an app showing the device's battery information

Receiving Battery Updates

In order to receive the updated information from the battery we need to register a broadcast receiver and an intent filter. An intent filter tells the native battery app that our app is listening for its changes.
This is how we register a receiver with an intent filter:

this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


In the code above the receiver is an object we are going to make called batteryInfoReceiver, the intent filter is listening for the action battery changed.
Our receiver object is the following:
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {

  @Override


  public void onReceive(Context context, Intent intent) {


                  // this is where we deal with the data sent from the battery.


  }


 };



Battery Manager API
If you look at the battery manager api yo see that it has many constants that start with the word EXTRA_, these extras is the data we are able to get from the battery and also the type (int, String etc).


Storing the battery info in variables with matching types

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

int  icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,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  scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);

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);


We can easily display the data in the views, the icon_small variable holds a resource id with the battery’s image:


Displaying data in text and image view

batteryInfo.setText(

 "Health: "+health+"\n"+

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

 "Level: "+level+"\n"+

 "Plugged: "+plugged+"\n"+

 "Present: "+present+"\n"+

 "Scale: "+scale+"\n"+

 "Status: "+status+"\n"+

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

 "Temperature: "+temperature+"\n"+

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

imageBatteryState.setImageResource(icon_small);

This is the full Java code.


BatteryInformation.java


package com.yoursite.batteryinformation;

 

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.widget.ImageView;

import android.widget.TextView;

 

 

public class BatteryInformation extends Activity {

 private TextView batteryInfo;

 private ImageView imageBatteryState;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        batteryInfo=(TextView)findViewById(R.id.textViewBatteryInfo);

        imageBatteryState=(ImageView)findViewById(R.id.imageViewBatteryState);

        

        this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    }

    

    private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {

  @Override

  public void onReceive(Context context, Intent intent) {

   

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

   int  icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,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  scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);

   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);

   

 

   batteryInfo.setText(

     "Health: "+health+"\n"+

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

     "Level: "+level+"\n"+

     "Plugged: "+plugged+"\n"+

     "Present: "+present+"\n"+

     "Scale: "+scale+"\n"+

     "Status: "+status+"\n"+

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

     "Temperature: "+temperature+"\n"+

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

   imageBatteryState.setImageResource(icon_small);

  }

 };

    

 

}


Battery Permissions

No special permissions are needed in the manifest file.
No related posts.

Nguon: suu tam
http://android.programmerguru.com/android-broadcast-receiver-example/
broadcast receiver battery low




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon