Android Question Phone Events

Declan

Well-Known Member
Licensed User
Longtime User
I am reading the Battery and Connection information.
In my Starter I have:
B4X:
Sub Process_Globals
    Dim PE As PhoneEvents
End Sub

Sub PE_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    CallSub2(Main, "ConnectionChanged", State)
    Log("ConnectivityChanged: " & NetworkType & ", state = " & State)
    Log(Intent.ExtrasToString)
End Sub

Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    CallSub2(Main, "BatteryChanged", Level)
    Log("BatteryChanged: Level = " & Level & ", Scale = " & Scale & ", Plugged = " & Plugged)
End Sub

In Activity("Main") I have:
B4X:
Sub ConnectionChanged(State As String)
    lblWifi.Text = State
End Sub

Sub BatteryChanged(Level As Int)
    lblBattery.Text = Level
End Sub
I keep getting the following error:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
ConnectivityChanged: WIFI, state = DISCONNECTED
Bundle[{networkInfo=[type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: <unknown ssid>, roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false], networkType=1, inetCondition=0, extraInfo=<unknown ssid>}]
Error occurred on line: 37 (Starter)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:857)
    at b4a.example.starter._pe_batterychanged(starter.java:156)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler$1.run(PhoneEvents.java:321)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5637)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.RuntimeException: java.lang.Exception: Sub BatteryChanged signature does not match expected signature.
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:333)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:282)
    ... 22 more
Caused by: java.lang.Exception: Sub BatteryChanged signature does not match expected signature.
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    ... 23 more
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy **

The error occurs here:
B4X:
CallSub2(Main, "BatteryChanged", Level)
 

DonManfred

Expert
Licensed User
Longtime User
The error is:
Sub BatteryChanged signature does not match expected signature.

1. initialize and give a eventname. Use this prefix for your eventsubs!

pe0066.png


B4X:
    PE.Initialize("PE")
End Sub
Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
 
End Sub
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I am reading the Battery and Connection information.
In my Starter I have:
B4X:
...
Sub PE_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
...
Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
...

Can you use a type name as a variable? You have "Intent as Intent" - I don't know if this is an issue or not, but to be safe maybe you should change it to something else like "thisIntent as Intent".

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The error is:

1. initialize and give a eventname. Use this prefix for your eventsubs!

pe0066.png


B4X:
    PE.Initialize("PE")
End Sub
Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)

End Sub
I think if initialization was the issue, he'd get an "Object should first be initialized (PhoneEvents)" error. :)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks Colin, that was it - I changed the intent name and all ok.
Good to know. I think that the documentation for that method signature is erroneous & perhaps if @Erel reads this, he will consider changing it so that others don't fall into the same trap.

- Colin.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Many thanks Colin for all your assistance.
One question:
The Battery and Connection information is obtained on a "Change" event.
Is it possible to use Phone Events to read the initial Connection Information when the Service is started?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Many thanks Colin for all your assistance.
One question:
The Battery and Connection information is obtained on a "Change" event.
Is it possible to use Phone Events to read the initial Connection Information when the Service is started?

I haven't really used Phone, PhoneEvents, etc... much - however looking at the documentation it seems you can get the connection state pretty easily & I'm sure there'll be a way to get the battery state as well. I'd suggest you have a look through the documentation here & see what you can find.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Where is the error?
A number of the Event signatures use "Intent as Intent" for a parameter declaration. It seems you can't use a type as a variable name. None of the other variable declarations use type names (eg: "Level as Int", "Package as String", "Plugged as Boolean"), so the signature examples that use "Intent as Intent" are likely to cause issues - especially if they are copied & pasted into code.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
This is not a problem. You can test it with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim in As Intent
   in.Initialize("ttt", "")
   Test(in)
End Sub

Sub Test(Intent As Intent)
   Log(Intent)
   Log(Intent.Action)
End Sub

Weird - looking at the original issue that Declan had & the error he was getting, & the fact that he reported that when he changed "Intent as Intent" to "thisIntent as Intent" the issue went away, it seemed that was his problem. But I ran his code this morning (after adding an initializer for PE) & it worked fine, so I don't know what the cause of the issue was.

It does seem strange that you can use "Intent" as an identifier, because you can't use other type names like Int or String. If you declare "Int as Int" or "String as Int", you get an error.

- Colin.
 
Upvote 0
Top