Search This Blog

Monday 2 September 2013

Android Battery Management API : Getting Battery Level and monitoring/determining its state programmatically using Battery Receiver

BatteryManager API exists in android to provide battery related function. It allow developer to develop application without draining battery of android user. Smart Developer never waste battery of android phone.
This tutorial teach you how to get current battery level of android phone and notify every time when battery level changes

Getting Current Battery Level

                IntentFilter mIntentFilter = new IntentFilter();  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_LOW);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_OKAY);  
                Intent batteryIntent = registerReceiver(null, mIntentFilter);  
                float batteryLevel = getBatteryLevel(batteryIntent);  
                Log.e("Battery Level", String.valueOf(batteryLevel));  

Creating Receiver for Observing/Monitoring Battery level changes


1) Create one Receiver and Register it with

      private class BatteryReceiver extends BroadcastReceiver {  
           @Override  
           public void onReceive(Context arg0, Intent arg1) {  
                if (arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)  
                          || arg1.getAction().equalsIgnoreCase(  
                                    Intent.ACTION_BATTERY_CHANGED)  
                          || arg1.getAction().equalsIgnoreCase(  
                                    Intent.ACTION_BATTERY_OKAY)) {  
                     int level = arg1.getIntExtra("level", 0);  
                     Toast.makeText(BatteryManagerC.this,  
                               "Current Battery " + level + " %", Toast.LENGTH_LONG)  
                               .show();  
                     mTextView  
                               .setText(String  
                                         .valueOf("Battery Level Change Detect through Receiver = "  
                                                   + level));  
                }  
           }  
      }  

2) Register it for Significant changes of battery level


      BatteryReceiver mArrow;  
      private class ThreadM implements Runnable {  
           @Override  
           public void run() {  
                mArrow = new BatteryReceiver();  
                IntentFilter mIntentFilter = new IntentFilter();  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_LOW);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_OKAY);  
                Intent batteryIntent = registerReceiver(mArrow, mIntentFilter);  
                batteryLevel = getBatteryLevel(batteryIntent);  
                Log.e("Battery Level", String.valueOf(batteryLevel));  
           }  
      }  

Complete code for listening and getting Battery level will be like this


 package com.test;  
 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.util.Log;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class BatteryManagerC extends Activity {  
      TextView mTextView;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
           mTextView = (TextView) findViewById(R.id.batterTest);  
           findViewById(R.id.getBattery).setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     mTextView.setText(String.valueOf(batteryLevel));  
                }  
           });  
           new Thread(new ThreadM()).start();  
      }  
      @Override  
      protected void onDestroy() {  
           super.onDestroy();  
           unregisterReceiver(mArrow);  
      }  
      BatteryReceiver mArrow;  
      private class ThreadM implements Runnable {  
           @Override  
           public void run() {  
                mArrow = new BatteryReceiver();  
                IntentFilter mIntentFilter = new IntentFilter();  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_LOW);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);  
                mIntentFilter.addAction(Intent.ACTION_BATTERY_OKAY);  
                Intent batteryIntent = registerReceiver(mArrow, mIntentFilter);  
                batteryLevel = getBatteryLevel(batteryIntent);  
                Log.e("Battery Level", String.valueOf(batteryLevel));  
           }  
      }  
      float batteryLevel;  
      private class BatteryReceiver extends BroadcastReceiver {  
           @Override  
           public void onReceive(Context arg0, Intent arg1) {  
                if (arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)  
                          || arg1.getAction().equalsIgnoreCase(  
                                    Intent.ACTION_BATTERY_CHANGED)  
                          || arg1.getAction().equalsIgnoreCase(  
                                    Intent.ACTION_BATTERY_OKAY)) {  
                     int level = arg1.getIntExtra("level", 0);  
                     Toast.makeText(BatteryManagerC.this,  
                               "Current Battery " + level + " %", Toast.LENGTH_LONG)  
                               .show();  
                     mTextView  
                               .setText(String  
                                         .valueOf("Battery Level Change Detect through Receiver = "  
                                                   + level));  
                }  
           }  
      }  
      public float getBatteryLevel(Intent batteryIntent) {  
           int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
           int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
           if (level == -1 || scale == -1) {  
                return 50.0f;  
           }  
           return ((float) level / (float) scale) * 100.0f;  
      }  
 }  

Finding the Charging State of Battery

      public void determineChargingState() {  
           IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);  
           Intent batteryStatus = registerReceiver(null, ifilter);  
           // Are we charging / charged?  
           int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);  
           boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING  
                     || status == BatteryManager.BATTERY_STATUS_FULL;  
           // How are we charging?  
           int chargePlug = batteryStatus.getIntExtra(  
                     BatteryManager.EXTRA_PLUGGED, -1);  
           boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;  
           boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;  
      }  

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code