It is a "partly" wrap for this Github project. If anyone has use for this I will add some more getters/setters that are not at present available in the original Github project (such as colors, etc).
Please note that I am driving this view via a B4A code module that makes use of inline Java code to extract some battery information.
Sample Code:
Code in module BatteryUtilities:
You need to enable the JavaObject library.
Plug your device into you PC/Laptop USB post and see the charging taking place (updated every 5 seconds). Unplug your device from the USB port and see the battery level being displayed inside the battery.
Plugged into USB port and charging:
Unplugged from USB port and showing current battery level:
The color of the battery should change to RED if the battery level drops below 35%
Posting the B4A lib files and a sample project
Please note that I am driving this view via a B4A code module that makes use of inline Java code to extract some battery information.
Sample Code:
B4X:
#Region Project Attributes
#ApplicationLabel: b4aBatteryView
#VersionCode: 1
#VersionName:
'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 bv1 As BatteryView
Private bu As BatteryUtilities
Dim batterystatus(11) As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
bu.Initialize
batterystatus = bu.BatteryInformation
bv1.Level = batterystatus(0)
If batterystatus(8) = 1 Then
bv1.Charging = True
Else
bv1.Charging = False
End If
t.Initialize("t", 20000)
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
bv1.Level = batterystatus(0)
bv1.Level = batterystatus(0)
If batterystatus(8) = 1 Then
bv1.Charging = True
Else
bv1.Charging = False
End If
End Sub
Code in module BatteryUtilities:
B4X:
'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 dummy As Int = 1
Dim batteryInfo(11) As Int
batteryInfo = nativeMe.RunMethod("getBatteryInformation",Null)
Return batteryInfo
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);
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;
}
#End If
You need to enable the JavaObject library.
Plug your device into you PC/Laptop USB post and see the charging taking place (updated every 5 seconds). Unplug your device from the USB port and see the battery level being displayed inside the battery.
Plugged into USB port and charging:
Unplugged from USB port and showing current battery level:
The color of the battery should change to RED if the battery level drops below 35%
Posting the B4A lib files and a sample project
Attachments
Last edited: