Getting Battery Details - Androhub

Battery Details Banner

Getting Battery Details

The main objective of this article is to help you to get all the details of the Battery in Android. Sometimes you need to read battery status and do some task on it. So today we will learn how to fetch all battery details.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Create activity_main.xml and add the following code. In this we are taking TextView for displaying Battery Details.

3. Now we will create a Broadcast Receiver naming Battery_Receiver.java to receive the Battery Status change. In this class we will read all Battery Data and change the TextView text in MainActivity class. In this Receiver we are using methods for getting Battery Details :

  • getPlugTypeString() : Used for Charging Type.
  • getHealthString() : Used for Health Type.
  • getStatusString()  Used for Battery Status.

4. Now create MainActivity.java and add the following code. In this class we need to follow below step:

  •     Register Receiver : Register above created receiver with IntentFilter – Intent.ACTION_BATTERY_CHANGED.

5. Finally, all done – now you can also read Battery Status.

Thanks. 🙂

 

Post comment

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