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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="fill_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="5dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <TextView android:id="@+id/battery_details" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textColor="@android:color/black" android:textSize="15sp" /> </LinearLayout> |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
package com.batterydetails_demo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.BatteryManager; import android.os.Bundle; import android.util.Log; /** * Created by SONU on 23/05/16. */ public class Battery_Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Get intent data of battery details boolean isPresent = intent.getBooleanExtra("present", false); String technology = intent.getStringExtra("technology"); int plugged = intent.getIntExtra("plugged", -1); int scale = intent.getIntExtra("scale", -1); int health = intent.getIntExtra("health", 0); int status = intent.getIntExtra("status", 0); int rawlevel = intent.getIntExtra("level", -1); int voltage = intent.getIntExtra("voltage", 0); int temperature = intent.getIntExtra("temperature", 0); int level = 0; Bundle bundle = intent.getExtras(); Log.i("BatteryLevel", bundle.toString()); if (isPresent) { if (rawlevel >= 0 && scale > 0) { level = (rawlevel * 100) / scale; } //Concatenate all details of battery String info = "Battery Level: " + level + "%\n"; info += ("Technology: " + technology + "\n"); info += ("Plugged: " + getPlugTypeString(plugged) + "\n"); info += ("Health: " + getHealthString(health) + "\n"); info += ("Status: " + getStatusString(status) + "\n"); info += ("Voltage: " + voltage + "\n"); info += ("Temperature: " + temperature + "\n"); new MainActivity().setBatteryLevelText(info + "\n\nBundle Data:\n" + bundle.toString());//Set battery text in main Activity } else new MainActivity().setBatteryLevelText("Battery not present!!!");// If no battery exist display this message } //Get Plug type for battery private String getPlugTypeString(int plugged) { String plugType = "Unknown"; switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: plugType = "AC"; break; case BatteryManager.BATTERY_PLUGGED_USB: plugType = "USB"; break; } return plugType; } //Get Health of battery private String getHealthString(int health) { String healthString = "Unknown"; switch (health) { case BatteryManager.BATTERY_HEALTH_DEAD: healthString = "Dead"; break; case BatteryManager.BATTERY_HEALTH_GOOD: healthString = "Good"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: healthString = "Over Voltage"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: healthString = "Over Heat"; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthString = "Failure"; break; } return healthString; } //Get Status of battery private String getStatusString(int status) { String statusString = "Unknown"; switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: statusString = "Charging"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusString = "Discharging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusString = "Full"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusString = "Not Charging"; break; } return statusString; } } |
4. Now create MainActivity.java and add the following code. In this class we need to follow below step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.batterydetails_demo; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private static TextView battery_details; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpToolbar(); battery_details = (TextView) findViewById(R.id.battery_details); registerBatteryLevelReceiver(); } //set up toolbar private void setUpToolbar() { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } //Register Battery receiver with intent filter private void registerBatteryLevelReceiver() { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(new Battery_Receiver(), filter); } //Set text of battery details public void setBatteryLevelText(String text) { battery_details.setText(text); } } |
5. Finally, all done – now you can also read Battery Status.
Thanks. 🙂
Subscribe to us and get the latest news.