How To Get All Battery Information Details In Android

In this example, How to learn get Android device battery information from our device. this tutorial is going to make use of an five Text View and on Image view controls. The sample is to help you to get all the information details of the battery in android. You can get more information from the Voltage Level, Status, Plugged, Temperature, USB, Connect or not connect. We can easily display the data in the views, the icon_variable holds a resource id with the battery's image. I hope you enjoy this tutorial see example and it would be helpful to you

1. MainActivity

import android.net.Uri;
import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.TextView;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends Activity {

    TextView level,technology,plugged,health,status,voltage,temp;
    ImageView imageView;
    AnimationDrawable ani;
    int temperature,voltages,statuss,healths,pluggeds;
    String technologys;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        level = (TextView) findViewById(R.id.level);
        technology = (TextView) findViewById(R.id.technology);
        plugged = (TextView) findViewById(R.id.plugged);
        health = (TextView) findViewById(R.id.health);
        status = (TextView) findViewById(R.id.status);
        voltage = (TextView) findViewById(R.id.voltage);
        temp = (TextView) findViewById(R.id.temp);


        imageView = (ImageView) findViewById(R.id.imagee);
        imageView.setBackgroundResource(R.drawable.gube);
        ani = (AnimationDrawable) imageView.getBackground();




        registerBatteryLevelReceiver();

        level.setText(getBatteryLevel()+ "%\n");

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(battery_receiver);

        super.onDestroy();
    }

    private BroadcastReceiver battery_receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isPresent = intent.getBooleanExtra("present", false);
            technologys = intent.getStringExtra("technology");
            pluggeds = intent.getIntExtra("plugged", -1);
            int scale = intent.getIntExtra("scale", -1);
            healths = intent.getIntExtra("health", 0);
            statuss = intent.getIntExtra("status", 0);
            int rawlevel = intent.getIntExtra("level", -1);
            voltages = intent.getIntExtra("voltage", 0);
            temperature = intent.getIntExtra("temperature", 0);
            int levels = 0;


            Bundle bundle = intent.getExtras();

            Log.i("BatteryLevel", bundle.toString());

            if (isPresent) {
                if (rawlevel >= 0 && scale > 0) {
                    levels = (rawlevel * 100) / scale;
                }
                String info = "Battery Level: " + level + "%\n";
                info += ("Technology: " + technology + "\n");
                info += ("Plugged: " + getPlugTypeString(pluggeds) + "\n");
                info += ("Health: " + getHealthString(healths) + "\n");
                info += ("Status: " + getStatusString(statuss) + "\n");
                info += ("Voltage: " + voltage + "\n");
                info += ("Temperature: " + temperature + "\n");
                health.setText(getHealthString(healths));
                technology.setText(technologys);
                plugged.setText(getPlugTypeString(pluggeds));
                status.setText(getStatusString(statuss));
               // voltage.setText(voltages);
               // temp.setText(temperature);
            } else {
            }
        }
    };

    private String getPlugTypeString(int plugged) {
        String plugType = "Unknown";

        switch (plugged) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                plugType = "AC";
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                plugType = "USB";
                break;
        }

        return plugType;
    }

    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;
    }

    // Batter Level
    public float getBatteryLevel() {
        Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        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;
    }

    private String getStatusString(int status) {
        String statusString = "Unknown";

        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                statusString = "Charging";
                ani.start();
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                statusString = "Discharging";
                ani.stop();
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                statusString = "Full";
                imageView.setBackgroundResource(R.drawable.f);
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                statusString = "Not Charging";
                imageView.setBackgroundResource(R.drawable.a);
                break;
        }

        return statusString;
    }



    private void registerBatteryLevelReceiver() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(battery_receiver, filter);



    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}

2. content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:showIn="@layout/activity_main">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="360dp"
        android:id="@+id/imagee"
        android:layout_marginTop="25dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="25sp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="395dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/level"
        android:layout_margin="10dp"
       android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp">

    <TextView
        android:id="@+id/technology"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="20dp"/>
        <TextView
            android:id="@+id/plugged"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="20dp"/>

        </LinearLayout>
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:id="@+id/health"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="20dp"/>
        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="20dp"/>

    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp">

            <TextView
                android:id="@+id/voltage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:gravity="center"
                android:visibility="gone"
                android:layout_weight="1"
                android:textSize="20dp"/>
            <TextView
                android:id="@+id/temp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:visibility="gone"
                android:gravity="center"
                android:layout_weight="1"
                android:textSize="20dp"/>

        </LinearLayout>
</LinearLayout>
    </RelativeLayout>