PhoneEvents.Initialize does not work after PhoneEvents.StopListening

corwin42

Expert
Licensed User
Longtime User
If I call PhoneEvents.StopListening I can not reinitialize the PE Object.

I have this small test program:

B4X:
'Activity 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
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.

   Dim edBattLevel As EditText
   Dim edStatus As EditText
   Dim btUpdate As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main1.bal")
   pe.Initialize("PE")   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
   ToastMessageShow("Data Updated", False)
   
   edBattLevel.Text = Level
   edStatus.Text = Intent.GetExtra("status")
   pe.StopListening
End Sub

Sub btUpdate_Click
   pe.Initialize("PE")
End Sub

I get this error if I press the update button:

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_btupdate_click (B4A line: 41)
pe.Initialize("PE")
java.lang.IllegalArgumentException: Receiver not registered: anywheresoftware.b4a.phone.PhoneEvents$14@4685a528
   at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:793)
   at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:845)
   at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
   at anywheresoftware.b4a.phone.PhoneEvents.StopListening(PhoneEvents.java:235)
   at anywheresoftware.b4a.phone.PhoneEvents.Initialize(PhoneEvents.java:196)
   at de.amberhome.batterymonitor.main._btupdate_click(main.java:212)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:89)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:85)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2408)
   at android.view.View$PerformClick.run(View.java:8816)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   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:860)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalArgumentException: Receiver not registered: anywheresoftware.b4a.phone.PhoneEvents$14@4685a528

It only works if I reDIM the pe Object in btUpdate_Click()
 

joseluis

Active Member
Licensed User
Longtime User
Erel I'm afraid that this is not fixed yet (or the bug came back).

I've attached the example by corwin42 as "PhoneEventsBug.zip" that demonstrates the same behaviour, at least in my phone.

I made a more complex example but I don't see the point in uploading it,that involves two activities and how they try to share the same PhoneEvents variable without success (because of this bug). And how the only solution I found was to declare one phoneevent variable on each activity, and locally redimming the variable to surpass the bug.

¿Could this be fixed for the next release?
 

Attachments

  • PhoneEventsBug.zip
    7.2 KB · Views: 238
Top