Android Question Moving java in-line code from Module to Class

Alessandro71

Well-Known Member
Licensed User
Longtime User
from a previous example I've found this java code to generate an event, which originally was in the Main module

B4X:
#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:
       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?
 

jkhazraji

Active Member
Licensed User
Longtime User
B4X:
event.InitializeNewInstance(Application.PackageName & ".classname$NetworkCallback", Null)
Class name in small letters.
 
Upvote 2
Solution

jkhazraji

Active Member
Licensed User
Longtime User
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:
'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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…