Android Question Global AsyncStreams for All Activities

Binary01

Active Member
Licensed User
Longtime User
Hi,

I created a android client app with 5 activities.
It can connect(send and receive msg) my pc software.

1. I place AsyncStreams at First Activity
2. I can send message from any activity to pc. e.g FirstActivity.myAsync.Write("xxx")
3. But I only receive message at First Activity that coded AsyncStreams.

Question is:
How can I receive message from pc at other activities (Not First Activity)?
How can I declare globally sub [Sub mySocket_NewData(Buffer() As Byte) ] that can access from any activities?


Some code of my app are as follow:
'In First Activity
At Process_Globals
Public mySocket As Socket
Public myAsync As AsyncStreams
Public myMsg As String

Sub mySocket_Connected(Successful As Boolean)
If Successful Then
myAsync.Initialize(mySocket.InputStream,mySocket.OutputStream,"mySocket")
btnWiFi.Enabled =False
If pnlWiFi.Visible =True Then pnlWiFi.Visible =False
Else
mySocket.Close
btnWiFi.Enabled =True
End If
End Sub

Sub mySocket_NewData(Buffer() As Byte)
Dim mLine As String
Dim v As Int
mLine = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
If mLine="good" Then
'bConnect=True
btnWiFi.Enabled=False
Else If mLine.SubString2(0,3) ="key" Then
v= mLine.SubString(mLine.Length-2)
slKey.SetValue(v)
End If

End Sub

'In Second Activity
I can send message from Second Activity as follow:
FirstActivity.myMsg ="Welcome"
FirstActivity.myAsync.Write(FirstActivity.myMsg.GetBytes("UTF8"))
 
Last edited:

nwhitfield

Active Member
Licensed User
Longtime User
Put the AsyncStreams activity in a service, and then your Activities can call a sub in the service to get the new data.

Or, if certain data only goes to certain activities, then the service can route it correctly and call an activity sub.

I have an app that does something like that with an httpjob; the principle's similar - in my case, I check the job name, and use a case statement to select which activity the results have to go back to; you might check the start of the data, or just go through the activities to see if they're paused or not - and then call a sub back in the appropriate activity to process the data.
 
Upvote 0

Binary01

Active Member
Licensed User
Longtime User
Thank nwhitfield,
I move the AsyncStreams activity in a service. Now I can receive and send to PC software from every app activity. I want to ask a question. Eg., my wifi connection is accidenlly disconnect. I send message to PC. I put try catch for send sub module. But I can not catch any error. How can I check connection?
 
Upvote 0
Top