B4A Library BatteryProgressView

Similar threads

B4A Library BatteryView
B4A Question BatteryManager
B4A Question Read Battery Voltage
B4A Question [SOLVED] Detect wireless charging?
B4A Question Reading mA current drain

Johan Schoeman

Licensed User
A wrap for this Github project. The is a B4A (with inline Java code) that drives the view with the current battery status - updated every 20 seconds via a timer.

Sample Code Main Activity:
#Region  Project Attributes
    #ApplicationLabel: b4aBatteryProgressView
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim t As Timer

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private bpv1 As BatteryProgressView
    Dim batterystatus(11) As Int
    Private bu As BatteryUtilities
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    t.Initialize("t", 20000)
    bpv1.ProgressColor = Colors.Red
    bpv1.MaxProgress = 100
    batterystatus = bu.BatteryInformation
    bpv1.Progress = batterystatus(0)
    bpv1.TextColor = Colors.Magenta
    bpv1.OuterCircleColor = Colors.Yellow
    bpv1.InnerCircleColor = Colors.DarkGray
    bpv1.PercentageTextColor = Colors.Green
    bpv1.SubTextColor = Colors.Cyan
End Sub

Sub Activity_Resume

    t.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    t.Enabled = False

End Sub

Sub t_tick
    batterystatus = bu.BatteryInformation
    bpv1.Progress = batterystatus(0)

End Sub
Sample Code of Class:
'Class module
Sub Class_Globals
    Private nativeMe As JavaObject
End Sub
'Initializes the object.
Public Sub Initialize
    nativeMe = Me
End Sub
'Return information about the battery status. It returns the following 11 values in an integer Array:
'EXTRA_LEVEL = current battery level, from 0 To EXTRA_SCALE.
'EXTRA_SCALE = the maximum battery level possible.
'EXTRA_HEALTH = the current health constant.
'EXTRA_ICON_SMALL = the resource ID of a small status bar icon indicating the current battery state.
'EXTRA_PLUGGED = whether the device is plugged into a Power source; 0 means it is on battery, other constants are different types of Power sources.
'EXTRA_STATUS = the current status constant.
'EXTRA_TEMPERATURE = the current battery temperature.
'EXTRA_VOLTAGE = the current battery voltage level.
'A value indicating if the battery is being charged or fully charged (If neither it returns 0 Else it returns 1)
'A value indicating if it is charging via USB (0 = Not USB, 2 = USB)
'A value indicating if it is charging via AC (0 = Not AC, 1 = AC)
Public Sub getBatteryInformation () As Int()

    Dim batteryInfo(11)  As Int
    batteryInfo = nativeMe.RunMethod("getBatteryInformation",Null)
    Return batteryInfo

End Sub

Public Sub getBatteryTechnolgy() As String

    Dim batterytech As String
    batterytech = nativeMe.RunMethod("getBatteryTechnology",Null)
    Return batterytech

End Sub

#If Java

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;

  public int[] getBatteryInformation() {

    int[] mybat = new int[11];
    Intent batteryIntent = ba.context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        mybat[0] = level;
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        mybat[1] = scale;
        int health = batteryIntent.getIntExtra(BatteryManager.EXTRA_HEALTH,-1);
        mybat[2] = health;
        int icon_small = batteryIntent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,-1);
        mybat[3] = icon_small;
        int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
        mybat[4] = plugged;
//        boolean present = batteryIntent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
        int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
        mybat[5] = status;
//        String technology = batteryIntent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
//        BA.Log("Technology = " + technology);
        int temperature = batteryIntent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,-1);
        mybat[6] = temperature;
        int voltage = batteryIntent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,-1);
        mybat[7] = voltage;
//        int ac = batteryIntent.getIntExtra("plugged",BatteryManager.BATTERY_PLUGGED_AC);
//        mybat[8] = ac;
//        int usb = batteryIntent.getIntExtra("plugged",BatteryManager.BATTERY_PLUGGED_USB);
//        mybat[9] = usb;

        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;
        mybat[8] = 0;
        if (isCharging == true) {
           mybat[8] = 1;

        // How are we charging?
        mybat[9] = 0;
        mybat[10] = 0;
        int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        if (usbCharge == true) {
           mybat[9] = 2;

        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
        if (acCharge == true) {
           mybat[10] = 1;

        return mybat;
   public String getBatteryTechnology() {

        Intent batteryIntent = ba.context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

        String technology = batteryIntent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);

        return technology;

#End If