Check/Get battery status in android programmatically

How to detect battery is charging, not charging, discharge and full and show on screen on button click.

Some times when application developer needs develop application related to mobile charging, discharging and full charging detection then BatteryManager class gives us 5 different type of options to check battery status and developer can do certain task on particular status conformation. So here is the complete step by step tutorial for Check/Get battery status in android programmatically.

android-project-download-code-button

Following 5 types of status are available in android devices :

  1. BATTERY_STATUS_CHARGING.
  2. BATTERY_STATUS_DISCHARGING.
  3. BATTERY_STATUS_FULL.
  4.  BATTERY_STATUS_UNKNOWN.
  5. BATTERY_STATUS_NOT_CHARGING.

How to Check/Get battery status in android programmatically.

Code for MainActivity.java file.

 package com.batterystatus_android_examples.com;
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.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Button button;
 IntentFilter intentfilter;
 int deviceStatus;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 MainActivity.this.registerReceiver(broadcastreceiver,intentfilter);
 
 }
 });
 
 }
 
 private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 
 deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
 
 if(deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING){
 
 textview.setText("Battery Status = Charging ");
 
 }
 
 if(deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){
 
 textview.setText("Battery Status = Discharging ");
 
 }
 
 if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL){
 
 textview.setText("Battery Status = Battery Full ");
 
 }
 
 if(deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN){
 
 textview.setText("Battery Status = Unknown ");
 }
 
 
 if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
 
 textview.setText("Battery Status = Not Charging ");
 
 }

 }
 };
 

}

Code for activity_main.xml layout file.

 <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="com.batterystatus_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Status"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:text="Click here to Get battery status in android programmatically" />

</RelativeLayout>

Screenshot:

Check/Get battery status in android programmatically

Click here to download Check/Get battery status in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *