Android Question Problems with FTP and Home Button

Patrick Clark

Active Member
Licensed User
I am struggling to understand how to manage ftp when the user taps the Home Button.

I have the following

B4X:
For i = 1 to 100 'don't worry why, it's just for testing
        Dim sf As Object = FTP.DownloadFile(SourceFile False, PathSettings, TargetFile)
        Wait For (sf) FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
        If Success Then
            Log("file was downloaded successfully")
        Else
            Log("Error uploading file")
        End If
Next

FTP.List("/")

If this runs with a large source file and the Home key is pressed a few things happen

1. I get a lot of errors about too many queued events ftp_downloadprogress
2. The 100 files don't all get downloded
3. FTP.List and it's associated completed sub never get executed

Obviously the App comes back into Activity.Resume but I don't think I can put this code in there as the loop would be started all over again

Any help would be greatly appreciated.
(Still trying to get my head around Async coding)


Basically I cannot continue with application logic until the FTP task has completed but I never know when that happens if the user does something unexpected
 
Last edited:

josejad

Expert
Licensed User
Longtime User
Hi:

Take a look to the Android life cycle, it's the expected behaviour your activity can be killed if it's not in foreground.

If you want your FTP download/upload keeps running, you should implement it in a service.
Search for "ftp service" to search some examples.
 
Upvote 0

Patrick Clark

Active Member
Licensed User
Thanks, putting it in a service fixed the ftp job not completing but i still don't understand how I know the task has completed in the calling activity when the app always returns to Activity.Resume
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
You can have for example, a variable in Starter:
B4X:
Dim UploadCompleted As Boolean
UploadCompleted = False
In your service, you could have:
B4X:
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log("FTP_UploadCompleted")
    Starter.UploadCompleted = True
End Sub

Then, in you Activity_Resume
B4X:
If Starter.UploadCompleted Then
...

Maybe there's a better solution...
 
Upvote 0

Patrick Clark

Active Member
Licensed User
Yes, that is ok if there is only one thing that the activity is doing but for example

B4X:
Use FTP to query a file timestamp
if that date/time is older than a stored value then
    use FTP to download the newer file
    update the locally held timestamp
end if
Process the data in the current file

Step 2 cannot be executed until step 1 is complete
Step 6 cannot be executed until previous steps have completed and the file is know to be the current version

I cannot even begin to think how that could be coded using flags in Activity.Resume
 
Upvote 0

Patrick Clark

Active Member
Licensed User
It's messy and I don't like it but this seems to work. At least I can get on with my project now while I look for a better answer

B4X:
    fv = -1
    CallSub2(vptftp,"GetFileVersion","PC.config")
    Do While fv = -1
        Sleep(1)
    Loop
    Label1.Text = "Config "&fv
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is not perfect.
The label will not be updated if the activity is paused.

Create a "StateUpdated" sub. In this sub you should get the data that you need from the starter service.
Call this sub with CallSubDelayed(Main, "StateUpdated") whenever needed.
Call this sub from Activity_Resume.
 
Upvote 0

Patrick Clark

Active Member
Licensed User
It does. If i hit home while the sub is running I get in the log "sending message to waiting queue (sleep)"
and when I go back to the app the log says "running waiting messages (1)" and the label is updated.

I don't understand what you mean by a StateUpdated sub and if I put it in Activity.Resume surely the ftp job would get started again every time Activity.Resume is called.

How does this help the situation where. line 2 has to wait for line one to complete and line 6 must wait for all of the previous code before the app is in a state for it to do its work.

B4X:
Use FTP to query a file timestamp
if that date/time is older than a stored value then
    use FTP to download the newer file
    update the locally held timestamp
end if
Process the data in the current file
 
Upvote 0

Patrick Clark

Active Member
Licensed User
Thanks, I am disabling the back key so that is ok.

I've looked at your example but i still can't understand how that will help to type of logic flow described above when many task have to follow the previous tack and cannot be started until the previous task has finished.

I guess that is the joy of android programming!!! I will get my head around it!
 
Upvote 0
Top