get battery level and state in android programmatically

Sometimes when you are developing an android application, you may want to get current batter level or state.
For example:
Lets say you want to make a rule that if battery level is less than 15%, turn off all background processes in android.
In this post, we are going to see how to get current battery level and state in android.
There are five android battery state which you must know.

  • BatteryManager.BATTERY_STATUS_FULL

Source code:


click to begin
20KB .zip

 Step 1 :

Create an android application project named “GetBatteryInfoApp”.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Step 2 :

Change res ->layout -> activity_main.xml as below:

Step 3:

Change src/main/packageName/ as below:

As we want to get battery level change information, we need to register broadcast receiver and intent filter. An intent filter is used to tell native battery app that this app is listening for changes in native battery app.
Lets understand above code:


If you see the code we have used Intent.ACTION_BATTERY_CHANGED to get current battery info. It is sticky broadcast which contain battery charging state , level and other info. BatteryManager class contains all constant strings.
Sticky broadcast is broadcast which will remain active once registered.

Here we are registering created broadcastreceiver to the MainActivity and intent filter is listening for battery change events.

Run the app:

When you run above app on actual device, you will get below screen :

When you click on button, you will get below screen :

So it shows current battery state and level.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.