How do I access string content from a service in the main activity?

mistermentality

Active Member
Licensed User
Longtime User
I have tried searching but the posts I found seem to suggest I should be able to read the content of a service variable from the main activity so as I cannot I have to risk embarrassing myself and seek clarification.

I have a service called incomingdata to intercept an incoming sms. In the service a variable called message is set to hold the content of the sms and then the following code

CallSub(Main, "MessageReceived")

calls the sub named MessageReceived in the activity which performs tests on the content of the message variable.

However I cannot compile because I get an error saying that the variable message is undeclared (from the activity not the service).

How can I read the data contained within the service activities message variable?

Activity code (works fine without a service, removed irrelevant code for shorter post) is:

B4X:
Sub Activity_Create(FirstTime As Boolean)
'Code to display layout on screen removed

StartService(incomingdata) 
End Sub

Sub Activity_Resume

End Sub

Sub MessageReceived
ToastMessageShow(message, True)
' message is string variable from service module
End Sub

and the service code (called incomingdata) is:

B4X:
'Service 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
    Dim SI As SmsInterceptor
   Dim message, sentby As String
End Sub
Sub Service_Create
    PE.Initialize("PE")
    SI.Initialize("SI")
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
message = Body
CallSub(Main, "MessageReceived(")
End Sub

Sub Service_Destroy
    
End Sub

Hopefully someone will point out what I am doing wrong

Dave
 
Last edited:

klaus

Expert
Licensed User
Longtime User
In the Main activity you need to call the variable with the service's name as a prefix. If the service name is 'incomingdata' the variable name should be
incomingdata.message
ToastMessageShow(incomingdata.message, True)

Best regards.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
In the Main activity you need to call the variable with the service's name as a prefix. If the service name is 'incomingdata' the variable name should be
incomingdata.message
ToastMessageShow(incomingdata.message, True)

Best regards.

I can't believe I didn't figure that out. Thank you :)

Dave
 
Upvote 0
Top