Problem with PhoneEvent "BatteryChanged"

monki

Active Member
Licensed User
Longtime User
:sign0085:

Hallo,

I have a Problem with the Phone Event "BatteryChanged"
If the widget is the first time I pull on the Home Screen its works, the second time to add the Widget the code crashes.
Why?

here is the Code


ub Process_Globals
Dim rv As RemoteViews
Dim ph As PhoneEvents
End Sub

Sub Service_Create
ph.Initialize("ev")
rv = ConfigureHomeWidget("Akkuw.bal", "rv", 0, "Akku1")

End Sub

Sub Service_Start (StartingIntent As Intent)

If rv.HandleWidgetEvents(StartingIntent) Then Return

End Sub

Sub rv_RequestUpdate
rv.UpdateWidget
End Sub

Sub rv_Disabled
ph.StopListening
StopService("")
End Sub

Sub Service_Destroy

End Sub

Sub ev_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
Log(level)
Log(Scale)
Log(Plugged)
'rv.SetProgress("ProgressBar1",level)
'rv.SetText("label1",level)
'rv.UpdateWidget
End Sub

Regards

PS:
Error Message in the Log
java.lang.IllegalArgumentException: Receiver not registered: anywheresoftware.b4a.phone.PhoneEvents$16@48270c68

Testet on HTC Desire & Samsung Galaxy TAB
 
Last edited:

monki

Active Member
Licensed User
Longtime User
PhoneEvent

Hi Erel,
thank you for the fast answer
here is a minimalist version with you can reproduce the error.

Best regards

monki
 

Attachments

  • Akku_Widget.zip
    3.7 KB · Views: 239
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim rv As RemoteViews
   Dim ph As PhoneEvents
   
End Sub

Sub Service_Create
    rv = ConfigureHomeWidget("Ak.bal", "rv", 0, "Akku1",True)
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
'   Dim ph As PhoneEvents  With this Redim it´s works
   ph.Initialize("ev")
   End Sub
You should only initialize ph once. This means that you should move ph.Initialze to Service_Create.
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
RE: PhoneEvent.....

@ Erel

I have moved ph.Initialze to Service_Create.
The same result. (HTC desire & Galaxy TAB 7)

???

B4X:
service_service_create (B4A line: 9)
ph.Initialize("ev")
java.lang.IllegalArgumentException: Receiver not registered: anywheresoftware.b4a.phone.PhoneEvents$16@4627b910
   at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:814)
   at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:822)
   at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
   at anywheresoftware.b4a.phone.PhoneEvents.StopListening(PhoneEvents.java:293)
   at anywheresoftware.b4a.phone.PhoneEvents.Initialize(PhoneEvents.java:252)
   at Akkuwidget.dfe.service._service_create(service.java:123)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:89)
   at Akkuwidget.dfe.service.onCreate(service.java:38)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:3125)
   at android.app.ActivityThread.access$3300(ActivityThread.java:135)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2202)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:144)
   at android.app.ActivityThread.main(ActivityThread.java:4937)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see it now. There is a bug in PhoneEvents.StopListening which sets it in a state that it cannot be initialized again. I've fixed it, however for now you will need to redim the variable:
B4X:
Sub rv_Disabled
   ph.StopListening
   Dim ph As PhoneEvents
    StopService("")
End Sub
Note that the object will only start listening when you call Initialize, so there is no performance loss here.
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
Hallo Erel,
many thanks for the quick help.
A note in the documentation would be helpful.

Best regards

monki
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
Hey Erel, just made a little service that waits for these events and writes them to the log as a test. I can confirm that this bug is still there in b4a 1.9...

Here's my workaround service code:

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim pe As PhoneEvents
Dim Running As Boolean
End Sub

Sub Service_Create
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   Dim pev As PhoneEvents
   pev.Initialize("pe")
   pe = pev
   Running = True
End Sub

Sub Service_Destroy
   pe.StopListening
   Running = False
End Sub

Sub pe_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
Log("Level: " & Level & ", Scale: " & Scale & ", Plugged: " & Plugged)
End Sub
 
Upvote 0
Top