Getting Battery Details

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. 🙂

Download Source Code – >

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)