How to get Battery Info using Broadcasr receiver in Android

Similarly, you can check the battery charge level. You can find the current battery charge by extracting the current battery level. Android BatteryManager class provides the battery information like status, health, level, temperature, plugged in status,voltage level etc.

Create New Project

1. First create a new project in Android Studio (File => New Project)

activity_main.xml

									   
   
   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/battery_level1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Battery Level"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Battery Status"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_heath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="batter_heath"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_technology"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="battery_technology"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_voltage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="battery_voltage"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_temperature_Celsius"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="battery_temperature"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/battery_temp_fahrenheit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="battery_temp_fahrenheit"
        android:textSize="20dp" />

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_gravity="center"
        android:minWidth="200dip"
        android:minHeight="150dip"
        android:max="100"
        style="?android:attr/progressBarStyleHorizontal"/>

</LinearLayout>

MainActivity.java


package com.example.narendra.batteryinfo;

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.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView battery_level1, battery_Level2, Battery_status, Battery_heath, Battery_technology,
            battery_voltage, battery_temperature_Celsius, battery_temp_fahrenheit;
    Context context;
    ProgressBar progressBar;

    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
            int heath = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);
            String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
            int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
            final float temp = (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)) / 10.0f; //temperature_unit in Celsius

            final float newTemp = (temp * 1.8f) + 32.0f; // You can use is if you need TEMPERATURE in fahrenheit

            progressBar.setProgress(level);
            battery_level1.setText("Level:" + level + "%");
            Battery_status.setText("Status:" + getStatusString(status));
            Battery_heath.setText("Heath:" + getHealthString(heath));
            Battery_technology.setText("Technology:" + technology);
            battery_voltage.setText("Voltage:" + voltage);
            battery_temperature_Celsius.setText("Temperature_Celsius:" + temp);
            battery_temp_fahrenheit.setText("Temperature_fahrenheit:" + newTemp);

            Log.e("-----", "battery_level1" + level);
            Log.e("-----", "Battery_status" + getStatusString(status));
            Log.e("-----", "Battery_heath" + getHealthString(heath));
            Log.e("-----", "Battery_technology" + technology);
            Log.e("-----", "battery_voltage" + voltage);
            Log.e("-----", "Temperature_Celsius" + temp);
            Log.e("-----", "Temperature_fahrenheit" + newTemp);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        battery_level1 = (TextView) findViewById(R.id.battery_level1);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        Battery_status = (TextView) findViewById(R.id.battery_status);
        Battery_heath = (TextView) findViewById(R.id.battery_heath);
        Battery_technology = (TextView) findViewById(R.id.battery_technology);
        battery_voltage = (TextView) findViewById(R.id.battery_voltage);
        battery_temperature_Celsius = (TextView) findViewById(R.id.battery_temperature_Celsius);
        battery_temp_fahrenheit = (TextView) findViewById(R.id.battery_temp_fahrenheit);

        registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    }

    private String getHealthString(int health) {
        String healthString = "Unknown";
        switch (health) {
            case BatteryManager.BATTERY_HEALTH_DEAD:
                healthString = "Dead";
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                healthString = "Good";
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                healthString = "Over Voltage";
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                healthString = "Over Heat";
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                healthString = "Failure";
                break;
        }
        return healthString;
    }

    private String getStatusString(int status) {
        String statusString = "Unknown";
        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                statusString = "Charging";
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                statusString = "Discharging";
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                statusString = "Full";
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                statusString = "Not Charging";
                break;
        }
        return statusString;
    }
}
	

Output :

Tags

  • Android

  • Android Studio

  • Android development

  • Java