Android Question running Asynstream in a service

Addo

Well-Known Member
Licensed User
i am trying to run tcp connection in a service all works fine until the app goes to background

i use callsub to update GUI when i bring the app to background the gui is not getting updated

i thought to handle this by using a list to hold on the received data and handle them when i go back to foreground

i do something like this in the service

B4X:
Public Sub handlebgcmd

If CMDLISTS.Size > 0 Then
For i = 0 To CMDLISTS.Size - 1
Handlcmdstrings(CMDLISTS.Get(i))
Sleep(0)
Next

CMDLISTS.Clear
        
End If

isbackground = False


End Sub

Public Sub Handlcmdstrings(CMDS As String)
Dim paramnum() As String



paramnum = Regex.Split("\&", CMDS)

If paramnum(0) = "ver" Then
Log(paramnum(1))
    
Else If paramnum(0) = "connected" Then
CallSub2(Main,"Displayconnect",paramnum(1))

    
End If
    
End Sub


Public Sub NEWDATA(data() As Byte)
    
Dim arrvmsg As String



arrvmsg = BytesToString(data, 0, data.Length, "UTF-8")
arrvmsg = arrvmsg.Trim


If arrvmsg.Length = 0 Then
Log("Error")
Return
End If

If isbackground = True Then
CMDLISTS.Add(arrvmsg)   
Return
End If

Handlcmdstrings(arrvmsg)





End Sub

and i do this in the main activity

B4X:
Sub Activity_Resume


CallSub(Service, "handlebgcmd")


End Sub


Sub Activity_Pause (UserClosed As Boolean)
    
If UserClosed = True Then
StopService(Service)   
Else
If UserClosed = False Then
Service.isbackground = True
End If
End If


End Sub

some times app crash when its comes back from background and the data gets conflicted

i feel its wrong way to handle the background operations how could i handle background operations with asyncstream correctly ?
 

Addo

Well-Known Member
Licensed User
i have watched the full tutorial i see no where in the video on how to deal with UI if the app in the background

the error were in Log(paramnum(1)) length due to some delay in foreground process i think what i am doing is not efficient way to handle the ui in background mode

You should check the logs and post the error message.

See the networking video tutorial: https://www.b4x.com/etp.html

It shows a good approach to managing the state and updating the UI.
 
Upvote 0

Addo

Well-Known Member
Licensed User
i am not sure if what iam doing is right due to some difficulty to understand

i have declared a list in the service in the process global with detection of background switch boolean variable

B4X:
Sub Process_Globals
    Public connected As Boolean
    Public isbackground As Boolean
    Dim CMDLISTS As List

then in the main activity on resum and pause i set isbackground boolean variable to true to start store the data that will accsess the UI while the app in the background

B4X:
Sub Activity_Resume


CallSub(Service, "handlebgcmd")


End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
If UserClosed = True Then
StopService(Service)  
Else
If UserClosed = False Then
Service.isbackground = True
End If
End If


End Sub


then in the data arrive i stop the accsess to any UI if isbackground variable is set to true


B4X:
Public Sub NEWDATA(data() As Byte)
   
Dim arrvmsg As String



arrvmsg = BytesToString(data, 0, data.Length, "UTF-8")


If arrvmsg.Length = 0 Then
Log("Error")
Return
End If

If isbackground = True Then
CMDLISTS.Add(arrvmsg)   'stop accsess to the ui handler if the app in the background and add the commands to a list instead
Return
End If

Handlcmdstrings(arrvmsg)


End Sub


then when the app comeback from background to forground on resum sub is calling the following

B4X:
Public Sub handlebgcmd
'here i start to loop through the arrived cmd list and accsess UI from handlcmdsstrings sub
If CMDLISTS.Size > 0 Then
For i = 0 To CMDLISTS.Size - 1
Handlcmdstrings(CMDLISTS.Get(i))
Sleep(0)
Next

CMDLISTS.Clear
       
End If

isbackground = False


End Sub


here problems starts

B4X:
'when call this sub in the loop the paramnum splitter gets conflict with the newdata arrived while adding the background cmd list
Public Sub Handlcmdstrings(CMDS As String)
Dim paramnum() As String



paramnum = Regex.Split("\&", CMDS)

If paramnum(0) = "ver" Then
Log(paramnum(1))
   
Else If paramnum(0) = "connected" Then
CallSub2(main,"Displayconnected",paramnum(1))
   
End If
   
End Sub
 
Upvote 0
Top