Android Question Upload files from service while showing progress bar

josejad

Expert
Licensed User
Longtime User
Hi everyone:

I made a small app for my company, learning at the same time I was making it.
Now I want to improve it with the things I'm learning and reading.

With the app, we make a lot of pictures (at about 200), and we upload them to a FTP server.
I made this with a resumable sub from my activity, but now I understand this is not the best way and I should move the FTP upload part to a service, in order not to be resumed if the screens locks or something like that. (and I should separate the logic code from the UI code)

I've been reading services tutorial and some FTP services threads, but I still have some questions.
My resumable sub, shows a modal diag with a progress bar, and I think services can't show UI's elements.

So, what would be the rigth way of doind this?

- Call the service from my activity, and let the service do all the job. I have a problem with this way, how can I know the progress? Can show in notification bar a progress bar like last versions of whatsapp while you listen an audio?

- Call the service from my activity, show the progress bar from the activity, and update it passing info throught globals variables between service and activity. (I think this is the rigth way)

- Start the upload from my activity, and call the service in activity resume to continue with the left files

Here is the code from my resumable sub.

B4X:
Sub UploadSite_Click
    Dim FileName As String
    Awake.KeepAlive(True) 
    Dim cmd As Object = FTP.SendCommand("MKD","/web/fotos/" & FileName)
    Wait For (cmd) FTP_CommandCompleted(ServerPath As String, Success As Boolean)
'    If Success Then
'        Log("Folder created")
'    Else
'        Log("Error creating folder")
'    End If
    'Creamos el panel Modal
    If FileList.Size > 0 Then
        Dim dftp As Object = FTPDialog.ShowAsync("Uploading files", "", "", "", LoadBitmap(File.DirAssets, "upload.png"), True)
        FTPDialog.SetSize(100%x, 250dip)
        Wait For (dftp) Dialog_Ready(pnl As Panel)
        pnl.LoadLayout("ModalFTP")
        'lbError.Text = "Errores: "
        Dim ErrorsList As List
        ErrorsList.Initialize
        For i=0 To ErrorsList.Size-1
            Archivo = FileList.Get(i)
            lbProgreso.Text = "Uploading file " & i & " de " & FileList.Size
            lbError.Text = "Current file: " & Archivo
            Dim sf As Object = FTP.UploadFile(Folder, File, False, "/web/fotos/" & FileName)
            Wait For (sf) FTP_UploadCompleted(ServerPath As String, Success As Boolean)
            If Success Then
                Log("file " & FileName& " was uploaded successfully")
            Else
                Log("Error uploading file " & FileName)
                listaErrores.Add(Archivo)
               
            End If
            ProgressBar1.Progress = 100 * (i+1) / FileList.Size
            If ProgressBar1.Progress = 100 Then
                FTPDialog.CloseDialog (DialogResponse.POSITIVE)
            End If
        Next
        Else
            ToastMessageShow("No pictures files", False)
    End If
    If ErrorsList.Size > 0 Then 
        MsgboxAsync("These files can't be uploaded: " & ErrorList, "E R R O R")
    End If
End Sub
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1024) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1024) & "KB"
   
    'Log(s)
End Sub
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log("Error FTP: " & LastException.Message)
    If Success = True Then ProgressBar1.Progress = 100
    Awake.ReleaseKeepAlive
End Sub

Thanks in advance¡¡
 

DonManfred

Expert
Licensed User
Longtime User
and I think services can't show UI's elements
you are right.
Check the GPS-Tutorial Example. It shows how to update the ui from a Service (calling a Method in the activity which then does the UI-Update)
 
Upvote 0
Top