How to get Android Device Battery Information

How to get Android Device Battery Information

In this tutorial, we are going to learn how to get Android Device battery information from our device. This is part of the series of android development tutorials that will focus on ways to access information from android device.

 

We are going to make use of an ImageView and three TextView controls. The ImageView will hold an image of a battery while the TextViews will display the battery status level, battery voltage level and the battery temperature level.

 

You can get more information from the battery but in this tutorial we will only focus on the three battery property mentioned above. You can read more about android device battery and its associative information in the android officially developer website.

 

In order to understand what we are out to achieve, we are going to have a look at the screen shot of the application we will create below.

voltage

Before we start, it is important for you to understand the tools and environment I used in this application development tutorial. Feel free to use any tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 22

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidBattery

Package: com.inducesmile.androidbattery

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button.

If you are using Android Studio as your choice IDE, the new project will create a default

MainActivity.java file and its corresponding layout file. Open the two files because we are going to

make use of them.

Now, open the activity_main.xml layout, we are going to add all the Views we listed above. The code snippet layout code is shown below.

<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">


    <ImageView
        android:id="@+id/battery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/battery"
        android:layout_marginTop="48dp" />

    <TextView
        android:id="@+id/battery_level"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_below="@+id/battery"
        android:layout_marginTop="24dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/voltage_level"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_below="@+id/battery_level"
        android:layout_marginTop="24dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/temperature_level"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_below="@+id/voltage_level"
        android:layout_marginTop="24dp"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Now that we are done with the layout file for the application, we will move over to the MainActivity.java file. Open the MainActivity.java file if it is not yet open in your IDE. We are going to get the handles for all out TextViews.

In order to get the android device battery information, an inner class of BroadcastReciever is created. In the onReceive() method of the class, we will use the intent parameter to retrieve the information we are interested in.

The complete source code for the MainActivity.java is shown below.

package inducesmile.com.androidbattery;

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


public class MainActivity extends ActionBarActivity {

    private TextView batteryLevel;
    private TextView voltageLevel;
    private TextView temperatureLevel;

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

        batteryLevel = (TextView)findViewById(R.id.battery_level);
        voltageLevel = (TextView)findViewById(R.id.voltage_level);
        temperatureLevel = (TextView)findViewById(R.id.temperature_level);

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent intent) {
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int voltage = intent.getIntExtra("voltage", 0);
            int temperature = intent.getIntExtra("temperature", 0);
            batteryLevel.setText("Battery Status: " + String.valueOf(level) + "%");
            voltageLevel.setText("Battery Voltage: " + String.valueOf(voltage));
            double temps = (double)temperature / 10;
            temperatureLevel.setText("Battery Temperature: " + String.valueOf(temps));
        }
    };
}

Now, when you run your application you will see the interface that looks similar to the sample that was shown earlier on.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

If you like this tutorial, please kindly download my new android app in Google play store by clicking the image below

OTHER INTERESTING POSTS:

Add a Comment