How To Get Battery State/level (Percentage) in Android ?

To Get Battery state or percentage of charging in android is really easy using Battery Manager class of Android framework.you can get battery percentage,last charged and all other information using Battery Manager class. 

To get This information i have created two methods. one is for getting percentage and another one is for getting charging state of phone. weather it is connected to charger or usb cable that will be also checked in this method.

Steps

  1. Create New Application and give name it GetBattery and Do next. 
    1. minSdkVersion="17” 
    2. targetSdkVersion="17". 
  2. Give activity name – Main.java and main.xml. 
  3. Go to main.xml and paste the below code.

main.xml



    

    

    

    


Well now Layout is set for your application. i used simple 4 textview for displaying battery percentage and charging status but you can use it in your application as you want.now paste below code in your java class file to get desired output.
Main.java
package com.androprogrammer.getbattery;

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.view.Menu;
import android.widget.TextView;

public class Main extends Activity {

 TextView batteryPercent, ChargingState;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  batteryPercent = (TextView) findViewById(R.id.tv_percentage);
  ChargingState = (TextView) findViewById(R.id.tv_state);
  getBatteryPercentage();
  if(isPhonePluggedIn(getApplicationContext()).compareToIgnoreCase("yes")== 0)
  {
   ChargingState.setText("yes");
  }
  else
   ChargingState.setText("no");
  
 }

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

 private void getBatteryPercentage() {
  
  
  BroadcastReceiver batteryLevel = new BroadcastReceiver() {
   
   public void onReceive(Context context, Intent intent) {
    context.unregisterReceiver(this);
    int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int level= -1;
    if (currentLevel >= 0 && scale > 0) {
     level = (currentLevel * 100) / scale;
    }
    batteryPercent.setText(level + "%");
   }
  };
  
  IntentFilter batteryLevelFilter = new IntentFilter(
    Intent.ACTION_BATTERY_CHANGED);
  registerReceiver(batteryLevel, batteryLevelFilter);
  
 }

 public static String isPhonePluggedIn(Context context) {
  boolean charging = false;
  String result = "No";
  final Intent batteryIntent = context.registerReceiver(null,
    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  boolean batteryCharge = status == BatteryManager.BATTERY_STATUS_CHARGING;

  int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

  if (batteryCharge)
   charging = true;
  if (usbCharge)
   charging = true;
  if (acCharge)
   charging = true;

  if (charging){
   result = "Yes";
   
  }
  return result;
 }
}

As you can see in above code i have used different methods of BatteryManager class for getting all stats in isPhonePluggedIn method. to get these value and run broad cast intent you have to add following permissions in your menifest file.
<uses-permission android:name="android.permission.BATTERY_STATS"/><uses-permission android:name="android.permission.BROADCAST_STICKY"/>
Screen Shot

so That's it form my site now its your turn run your application. if you find this article helpful like us on Google+ or facebook fan page. 

2 comments :