Android Question How to detect power outage?

juacochero

Member
Licensed User
Hi everyone!
I need to build an app that sends an SMS message when the phone looses power connection.
Is there a library that helps detect when the phone is unplugged, and triggers an event?
Thank you!
 

Didier9

Active Member
Licensed User
I use this:

B4X:
Sub PhoneEvent_BatteryChanged( Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent )
    PluggedStatus = Plugged   
    ChargeLevel = Level
    If PluggedStatus = False And ChargeLevel < 50 Then
        pws.KeepAlive( False )
    Else
        pws.KeepAlive( True )
    End If
End Sub ' PhoneEvent_BatteryChanged()
Plugged tells you if the phone is charging.
In this example, I run the pws.KeepAlive() function depending on the charge level.
The event gets called when the charging status or charge level changes.
 
Top