Mobile Battery Status Display
By: Sameer Sitoula
Tags: Battery Life, Mobile, Battery, Battery Health, Nano Battery Application

Introduction

This application which we have developed on the android application is used to display the battery status when build and run on a computer or mobile device. The main idea of this app to show the various battery related things which are important for the device. This app is used to display various things:-

a.) Maximum charging voltage.

b.) Battery health status.

c.) Maximum charging current.

d.) Status

e.) Charging or not.

f.) Temperature.

g.) Voltage.

h.) Low battery( if the battery is very low).

i.) Invalid charger( if charger type is not suited).

Creation of Android Project:

1. Create a new project in Android Studio from File ⇒ New Project and fill the project details.

2. Open build .gradle (app/build.gradle), set the minSdkVersion and targetSdkVersion to 22 or higher, and also don’t forget to include these dependencies in your app Gradle.

Create a package called battery_monitor which is used to monitor the battery. For determining the current charge status and battery properties. The BatteryManager broadcasts all battery and charging details in a sticky Intent that includes the charging status. Then register a BroadcastReceiver by simply calling registerReceiver passing in mBroadcastReceiver as the receiver as shown in the next snippet, the current battery status intent is returned. package com.example.battery_monitor;

import android.os.BatteryManager;

import android.os.Bundle;

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.util.Log;

import android.widget.TextView;

public class MainActivity extends Activity {

TextView textBatteryLevel = null;

String batteryLevelInfo = “Battery Level”;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textBatteryLevel = (TextView) findViewById(R.id.txtBatteryInfo);

registerBatteryLevelReceiver();

}

protected void onDestroy() {

unregisterReceiver(battery_receiver);

super.onDestroy();

}

The Battery Manager broadcasts is an action whenever the device is connected or disconnected from power. It’s important to receive these events even while your app isn’t running and alter your refresh rate accordingly. To extract both the current charging status and, if the device is being charged also check how we are charging whether it’s charging via USB, AC Charger, or Wireless. we have to maximize the rate of your background updates in the case where the device is connected to an AC charger, reduce the rate if the charge is over USB, and lower it further if the battery is discharging.

private BroadcastReceiver battery_receiver = new BroadcastReceiver() {

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 + “%

”;

info += (“Technology: “ + technology + “

”);

info += (“Plugged: “ + getPlugTypeString(plugged) + “

”);

info += (“Health: “ + getHealthString(health) + “

”);

info += (“Status: “ + getStatusString(status) + “

”);

info += (“Voltage: “ + voltage + “

”);

info += (“Temperature: “ + temperature + “

”);

setBatteryLevelText(info + “



” + bundle.toString());

} else {

setBatteryLevelText(“Battery not present!!!”);

}

}

};

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;

}

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;

}

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;

}

private void setBatteryLevelText(String text) {

textBatteryLevel.setText(text);

}

private void registerBatteryLevelReceiver() {

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

registerReceiver(battery_receiver, filter);

}

}

After the code part is completed save it in .apk extension and the link which contains this .apk file can be installed on a mobile phone to check the battery status of the mobile phone directly by opening an app.