THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of this post is to help you to get all the details of the Battery in Android.

 

You will get Final Output:

Battery Details

 

Step 1 registerBatteryLevelReceiver() method

First we need Intent Filter. Which response with Battery status change. Create New Method named registerBatteryLevelReceiver() into MainActivity class and call this method into onCreate(). And also add textview into Layout File for display the Battery data.

private void registerBatteryLevelReceiver() {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 
registerReceiver(battery_receiver, filter);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
textBatteryLevel = (TextView) findViewById(R.id.txtBatteryInfo);
 
registerBatteryLevelReceiver();
}
 

Step 2 onReceive() method

Then we need Broadcast Receiver for receiving data with Battery Status change. Past Following code outside the onCreate() Method.

private BroadcastReceiver battery_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
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;
}
 
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");
 
setBatteryLevelText(info + "\n\n" + bundle.toString());
} else {
setBatteryLevelText("Battery not present!!!");
}
}
};
 

Step 3 MainActivity.java

Now we add Three new Method into MainActivity class:

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

3.1 Plug Type

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;
}
 

3.2 Health Type

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;
}
 

3.3 Battery Status

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;
}
 

Step 4 setBatteryLevelText() method

Finally we need to set Battery data in textview.

private void setBatteryLevelText(String text) {
textBatteryLevel.setText(text);
}

For more details go on following links:

I hope you enjoy this tutorial and it would be helpful to you.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.

face mask Belial The Demon Headgear Pocket Staff Magic