Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): use newer APIs for battery level/status properties #13996

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

m1ga
Copy link
Contributor

@m1ga m1ga commented Feb 19, 2024

The current implementation of the battery level/state properties are returning the stored values from the BroadcastReceiver. Android introduced a way to query the value using the BatteryManager starting in 21 (level) and 26 (state).

Android docs:
https://developer.android.com/reference/android/os/BatteryManager.html#BATTERY_PROPERTY_STATUS
https://developer.android.com/reference/android/os/BatteryManager.html#BATTERY_PROPERTY_CAPACITY

Currently the initial value is -1 and if the receiver wasn't called yet (e.g. timing or it was somehow removed) it won't update the values.

The new way should still return a fresh value because it reads it when your fetch the property.

Update
Battery level: since 21 is the current minSDK I removed the if and just use the new value.

Test:

var win = Ti.UI.createWindow();
win.open();

win.addEventListener("open", function() {
	console.log("Level", Titanium.Platform.batteryLevel);
	console.log("Status", Titanium.Platform.batteryState);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant