This page looks best with JavaScript enabled

Get Battery Percentage Using Broadcast Receiver In Android

 ·  ☕ 2 min read  ·  ✍️ Adesh

Create your android app named as BatterPercentageDemo.

Add TextView in MainActivity Layout File

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/tvBattery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Create BatteryReceiver Java Class

Creae BatteryReceiver java class and extends it by BroadcastReceiver.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class BatteryReceiver extends BroadcastReceiver {

    TextView tv;
    BatteryReceiver(TextView tv){
        this.tv = tv;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        int batteryPercentage = intent.getIntExtra("level", 0);
        if(batteryPercentage != 0){
            tv.setText(batteryPercentage + "%");
        }
    }
}

Override the onReceive method and use the Intent to get the battery level.

Register Broadcast Receiver Programmatically In MainActivity Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class MainActivity extends AppCompatActivity {

    BatteryReceiver batteryReceiver;

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

        TextView tv = (TextView)findViewById(R.id.tvBattery);

        batteryReceiver = new BatteryReceiver(tv);
        registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(batteryReceiver);
    }
}

We have created the object of BatteryReceiver class and pass the TextView object in the constructor.
Use registerReceiver method to regsiter your batteryReceiver class with Intent Action ACTION_BATTERY_CHANGED.

Unregsiter Broadcast Receiver

Unregister your broadcast receiver class object batteryReceiver in onStop method.

1
2
3
4
5
 @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(batteryReceiver);
    }

Fix: update the compileSdk and targetSdk to 31 in build.gradle file.

Now, connect your android phone and run the app there. It will show the battery percentage in the app.

Share on

Adesh
WRITTEN BY
Adesh
Technical Architect