Alessandro71 Well-Known Member Licensed User Longtime User Jul 15, 2026 #1 from a previous example I've found this java code to generate an event, which originally was in the Main module B4X: Copy #if Java public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback { public void onAvailable(android.net.Network network) { processBA.raiseEventFromUI(null, "network_available", network); } } #End If the event is referenced with B4X: Copy Dim event As JavaObject event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null) manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event)) Wait For network_available (Network As Object) I understand that the ".main$NetworkCallback" string in the event.InitializeNewInstance call refers to the java code in the Main module. In a B4XPages world, where I'd like to put all the network related calls in a separate class (for example named : NetTools) how the string should be changed? Should it contain the Class name ("NetTools$NetworkCallback") or the object instance name ("toolnamexample$NetworkCallback") or some other mangling derived from B4A internal object structure?
from a previous example I've found this java code to generate an event, which originally was in the Main module B4X: Copy #if Java public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback { public void onAvailable(android.net.Network network) { processBA.raiseEventFromUI(null, "network_available", network); } } #End If the event is referenced with B4X: Copy Dim event As JavaObject event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null) manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event)) Wait For network_available (Network As Object) I understand that the ".main$NetworkCallback" string in the event.InitializeNewInstance call refers to the java code in the Main module. In a B4XPages world, where I'd like to put all the network related calls in a separate class (for example named : NetTools) how the string should be changed? Should it contain the Class name ("NetTools$NetworkCallback") or the object instance name ("toolnamexample$NetworkCallback") or some other mangling derived from B4A internal object structure?
Solution J J jkhazraji Jul 15, 2026 B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters.
B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters.
J jkhazraji Active Member Licensed User Longtime User Jul 15, 2026 #2 B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters. Upvote 2 Solution
B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters.
J jkhazraji Active Member Licensed User Longtime User Jul 15, 2026 #3 You can also put the following callback function as a sub in the created class, then initialize it from B4XMainPage class and call the function there: B4X: Copy 'in testClass: Sub Class_Globals End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize End Sub Sub networkCallBack Dim callback As JavaObject Log(GetType(Me)) callback.InitializeNewInstance(GetType(Me) & "$MyNetworkCallback", Array(Me)) '$ + class name. And pass Me to the constructor Wait For network_state (Available As Boolean, network As Object) Log(Available) End Sub 'Then in B4XMainPage: . . Private testClass As testClass . testClass.Initialize testClass.networkCallBack Upvote 0
You can also put the following callback function as a sub in the created class, then initialize it from B4XMainPage class and call the function there: B4X: Copy 'in testClass: Sub Class_Globals End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize End Sub Sub networkCallBack Dim callback As JavaObject Log(GetType(Me)) callback.InitializeNewInstance(GetType(Me) & "$MyNetworkCallback", Array(Me)) '$ + class name. And pass Me to the constructor Wait For network_state (Available As Boolean, network As Object) Log(Available) End Sub 'Then in B4XMainPage: . . Private testClass As testClass . testClass.Initialize testClass.networkCallBack
Alessandro71 Well-Known Member Licensed User Longtime User Jul 25, 2026 #4 jkhazraji said: B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters. Click to expand... as soon as I moved the java code from Main module to a separate class, i've got the following error error: cannot find symbol processBA.raiseEventFromUI(null, "network_available", network); ^ symbol: variable processBA location: class NetworkCallback Upvote 0
jkhazraji said: B4X: Copy event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null) Class name in small letters. Click to expand... as soon as I moved the java code from Main module to a separate class, i've got the following error error: cannot find symbol processBA.raiseEventFromUI(null, "network_available", network); ^ symbol: variable processBA location: class NetworkCallback
Alessandro71 Well-Known Member Licensed User Longtime User Jul 25, 2026 #5 substituting processBA with ba does not help either... error: non-static variable ba cannot be referenced from a static context ba.raiseEventFromUI(null, "network_available", network); ^ Note: src\com\powercruisecontrol\doiptestclient\main.java uses or overrides a deprecated API. Upvote 0
substituting processBA with ba does not help either... error: non-static variable ba cannot be referenced from a static context ba.raiseEventFromUI(null, "network_available", network); ^ Note: src\com\powercruisecontrol\doiptestclient\main.java uses or overrides a deprecated API.
Alessandro71 Well-Known Member Licensed User Longtime User Jul 25, 2026 #6 JAVA code by @Filippo from this thread https://www.b4x.com/android/forum/threads/question-about-wifinetworkspecifier-networkrequest.169820/ solved the issue Upvote 0
JAVA code by @Filippo from this thread https://www.b4x.com/android/forum/threads/question-about-wifinetworkspecifier-networkrequest.169820/ solved the issue