Amazon Gift Cards

Sunday, January 4, 2009

Getting Battery Information on Android

Getting the Battery Information in Android without Apps like AnyCut, Power Manager or System Monitor is one of the hardest task. You can try those if you have an Android phone, anyway here is how to get the Battery Information (some of the codes are undocumented).
public class Main extends Activity {
  private TextView contentTxt;
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    public void onReceive(Context arg0, Intent intent) {
      // TODO Auto-generated method stub
      int level = intent.getIntExtra("level", 0);
      contentTxt.setText(String.valueOf(level) + "%");

  public void onCreate(Bundle icicle) {
    contentTxt = (TextView) this.findViewById(;
    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

Quick Explanation
Here we listen to a Intent (ACTION_BATTERY_CHANGED) and register a receiver when the intent is fired. In ActionScript, this is i believe is DispatchEvent, and we would call mBatInfoReceiver and get the current level of our battery life and put that int value to our textfield or TextView.

Source Code
My Battery Status 1 Source

Here is the source where i got the this stuff working

Update History
   Jan 17, 2012 - Visual Update


Maxim Yudin said...

Hi, do you know how change screen brightness on G1 programatically? Btw, better will get battery level as here -

admin said...

try this article

lfmdk said...

Thanks for the article, my question is, to use a broadcast reciever, you will need to specify it in the manifest.xml, how do you do that?

Anonymous said...

Thank you almond, your Battery tutorial help me lot in my programing.
Now I am try to make a Application which show the remaining time of battery discharge.
Can you halp me out any way.
Thank you.
- Amit Parekh.

monmonja said...

@lfmdk this must be so late, but you could either post your receiver in xml or in java code

Unknown said...

That was great help! Thanks!

Unknown said...

hey thats really helping for me!!
I also tried to get the battery temperature and voltage but i got some weird values
battery temperature=293
what are these values and units??

Anonymous said...

Hey, something's wrong with the links. They reffer to missing content. The article is uncomplete now:(

monmonja said...

you could see the source here

same said...

i tried to so.But program is not running.Please suggest if any other setting need to be do for run this.

srizan said...

The tutorial is nice for new-bie. As it is here we are directly displaying it on the text view. But can we set into one variable and use it in the activity class on onCreate method?
Thanks in advance!!

srizan said...

nice tutorial ! Can we set the battery level into one variable and display it as a Toast in onCreate function of activity?

Anonymous said...

Try this if you need it in onCreate or as a Toast:

private static final String BATTERY_CAPACITY_SYSFS="/sys/class/power_supply/battery/capacity";

private static String readSysFsFile(String a_File) throws Exception
FileReader a_Reader = new FileReader(a_File);
int a_Char;
String a_Content = "";

while((a_Char = != -1)
// Ignore \n
if (a_Char != 10)
a_Content += Character.toString((char) a_Char);

return a_Content;

public static String getBatteryStatus()
String a_Capacity = readSysFsFile(BATTERY_CAPACITY_SYSFS);

if (a_Capacity.length() == 0)
a_Capacity = "Error";

return a_Capacity;
catch(Exception a_Ex)
return "Error";

Srizan said...

@anonymous, thanks it works but my question is , how to transfer the variable from bootreceiver to Activity on the same file !

Namor Alves said...

Thanks so much!