Android Question Passing a variable from Main to Service module

William Hunter

Active Member
Licensed User
Longtime User
I have a declared variable in a SERVICE module. I wish to use AHPreferenceActivity in the Main module to input the value of that variable, then in SubInitializeConnectVariables pass it to the SERVICE module for use there.

What is the proper syntax to do this? Any help greatly appreciated.
 

William Hunter

Active Member
Licensed User
Longtime User
You have to declare those variables in Process_Globals, then, from the service you can get the name and value like this:
B4X:
LocalVariable = Main.Myvariable
Thank you for your response NJDude. I think I didn’t express my query as well as I should have.

I have declared the variable in the service as below:
B4X:
Sub Process_Globals
     Public port As Int
End Sub

In the Main module I am using AHPreferenceActivity as a means of inputting the variable to the application. The code I am using is below:
B4X:
Sub InitializeConnectVariables
     ServerService.port = manager.GetString("edit1")
End Sub

I had thought this would pass the value from the Main module to the variable in the Service module. But, it doesn’t do this. I’m at a loss to understand how to do this. If you have any thoughts please pass them along.


Best regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
The above code is correct. It will set the value of 'port' variable in the service to the value of manager.GetString(...).
Thank you Erel. I was sure my code was correct. But, I was getting some strange behavior. The code below does pass the variable to the Service:
B4X:
Sub InitializeConnectVariables
  ServerService.port = manager.GetString("edit1")
  Log("Port used Main: " & ServerService.port) 'variable correctly fetched
  StartDialog = manager.GetBoolean("check1")
End Sub
What led me astray was that after initializing the connect variable, ServerService.port was no longer accessible for use in the Main module. The code below, in Sub Activity Create, did not display the port no, only a 0. I took this to mean that the variable was not passed to ServerService.port in Sub InitializeConnectVariables. Not so.
B4X:
lblIP.Text = "Device address: " & ssocket.GetMyWifiIP & ":" & ServerService.port
msgBoxURL = ssocket.GetMyWifiIP & ":" & ServerService.port
I am using your HttpServerExample. The only changes I have made are purely cosmetic. I ran into this problem when trying to use AHPreferenceActivity to input various settings. I have tried many ways to work around this, but to no avail. The strange thing about this is, if I manually give ServerService.port a value, in the service module, as in the code below, all is well.
B4X:
Public port As Int = 5555
Do you have any thoughts?

Regards
 
Upvote 0
Top