Android Question Yet another BatteryChanged question...

Nickelgrass

Active Member
Licensed User
Longtime User
Hi,
I have read a lot of posts to this subject but still I can't resolve my issue.

I have the following code that sends the battery level to a PHP skript in the internet.

B4X:
#Region  Service Attributes
    #StartAtBoot: true
#End Region

Sub Process_Globals
    Dim hc As HttpClient
    Dim pe As PhoneEvents
    Dim last As Int
End Sub

Sub Service_Create
    Dim n As Notification
    n.Initialize
    n.AutoCancel = False
    n.OnGoingEvent = True
    n.Vibrate = False
    n.Sound = False
    n.Light = False
    n.Icon = "icon"
    n.SetInfo("BatMon", "starting", Main)
    last = 0
    Service.StartForeground(1, n)
    hc.InitializeAcceptAll("hc")
    pe.Initialize("pe")
End Sub

Sub Service_Start (StartingIntent As Intent)
    pe.Initialize("pe")
End Sub

Sub Service_Destroy
    Service.StopForeground(1)
    Dim n As Notification
    n.Initialize
    n.Cancel(1)
    pe.StopListening
End Sub

Sub pe_BatteryChanged(Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    If last = Level Then
        StartServiceAt(Me, DateTime.Now + (DateTime.Ticksperhour * 4), True)
        Return
    End If
    Dim n As Notification
    n.Initialize
    n.AutoCancel = False
    n.OnGoingEvent = True
    Dim so As Boolean = False
    If Level < 10 Then     so = True
    n.Vibrate = so
    n.Sound = so
    n.Light = False
    n.Icon = "icon"
    n.SetInfo("BatMon", Level & "%", Main)
    n.Notify(1)
    last = Level
    upload(Level)
    StartServiceAt(Me, DateTime.Now + (DateTime.TicksPersecond * 4), True)
End Sub

Sub upload(level As Int)
    StartServiceAt(Me, DateTime.Now + (DateTime.Ticksperhour * 6), True)
    Dim hr As HttpRequest
    hr.InitializeGet("http://www.somesite.com/battery.php?lv=" & level & "&id=" & File.ReadString(File.DirInternal, "id.txt"))
    hc.Execute(hr, 1)
End Sub

Sub hc_ResponseSuccess (response As HttpResponse, TaskId As Int)
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
End Sub

But it still occasionally misses to fire the event. Also when stoping and restarting the service it does simply not allways fire. Even when starting the service in foreground it sometimes misses. What can this be? Is the Andriod system so inreliable on this?
The startserviceat is just a failsafe incase the level doesnt actually change for some time. But this is not the cause, neither is that I check the last value to the current. I have checked that.
 

Nickelgrass

Active Member
Licensed User
Longtime User
Thanks for the reply. I just put the pe.initialize in the service_start to see if it changed anything. But it was the same result with or without that occasional it gets missed. Especially when starting, the notification stays in "starting" state and no update is uploaded.
Regarding the second question: I thought that because I only send one get request it didnt really matter if I use the httpclient directly. What advantage would the httputils2 give in this case? There is no other data being transmited or received.
 
Upvote 0
Top