Basics
Sometimes it may be good to check what is the device battery level, for example during game session or before installing some extra stuff. Thus the user won’t have any problem during operation.
In the example below, we will make an app that shows if the device is charging or not and what is the current battery level.
Layout File
In the layout file activity_main.xml, we have two text views. The upper one shows charging status and lower one shows battery level.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:context=".MainActivity">
<TextView
android:id="@+id/chargingstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.300" />
<TextView
android:id="@+id/batterylevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Code for Checking The Battery Level
The MainActivity.java shows the actual code.
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String chargingStatusText;
String batteryLevelText;
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean charging = status == BatteryManager.BATTERY_STATUS_CHARGING;
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int batteryPct = Math.round(level / (float)scale * 100);
TextView chargingStatus = findViewById(R.id.chargingstatus);
TextView batteryLevel = findViewById(R.id.batterylevel);
if(charging) {
chargingStatusText = "Battery is charging.";
} else {
chargingStatusText = "Battery is not charging.";
}
batteryLevelText = "Battery level: " + batteryPct + "%";
chargingStatus.setText(chargingStatusText);
batteryLevel.setText(batteryLevelText);
}
};
IntentFilter filter
= new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, filter);
}
}
The battery level checking needs a broadcast receiver (line 20) that keeps listening for the battery information sent by the device.
When app receives the battery information, it runs the onReceive() function in line 22.
In lines 24 and 25, we define the strings to keep the battery information.
The actual information will be send to app inside an intent. In lines 27 to 30, we get the battery information from the intent and put it in the status, charging, level, and scale variables. In line 31, we calculate the battery level to be percent instead of given number.
In lines 34 and 35, we define the textViews where we will show the information.
In lines 36-40, we define if the battery is charging or not.
The string batteryLevelText in line 42 keeps the actual battery level. Finally, we will show the battery informationon the screen in lines 43 and 44.
Register Receiver
So how the device knows that it should send the battery information to our program? That happens in lines 48-50. There we register the receiver to listen to the battery information sent by the device.