Android Question OutputStream how can use wait

sigster

Active Member
Licensed User
Longtime User
Hi

How can I use Wait to tell me when I am finish to save the file so I can close the ProgressDialog

B4X:
Sub check_upgrade As ResumableSub

    Private AppVer As Int  = Application.VersionCode

    Dim sf As Object = Starter.mysql.ExecQueryAsync("mysql", "SELECT file,version from upgrade WHERE ID LIKE '" & "1" & "%'", Null)
    Wait For (sf) mysql_QueryComplete (Success As Boolean, Crsr As JdbcResultSet)   
    If Success Then   
        Do While Crsr.NextRow
            Dim b() As Byte = Crsr.GetBlob("file")
            Dim newversion As String = Crsr.GetString("version")
        Loop   
    End If
    If newversion = Null Then newversion = 0
    If AppVer < As Then
        Msgbox2Async("Do you want to download?","It is new version","Yes","","No",Null,False)         
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            ProgressDialogShow("Download")           
            saveAPK(b)
            Sleep(0)           
            ProgressDialogHide
        End If
    End If
    Return Success
End Sub

Public Sub saveAPK(data() As Byte) As ResumableSub
    Dim out As OutputStream = File.OpenOutput(Starter.Provider.SharedFolder, "tt.apk", False)
    out.WriteBytes(data, 0, data.Length)
    out.Close
    Return out
End Sub
 

Mahares

Expert
Licensed User
Longtime User
How can I use Wait to tell me when I am finish to save the file so I can close the ProgressDialog
Did you try something like this, not tested:
B4X:
If Result = DialogResponse.POSITIVE Then
        ProgressDialogShow("Download")
        Wait For (saveAPK(b)) Complete (Unused As Int)
        ProgressDialogHide
    End If
B4X:
Sub saveAPK(data() As Byte) As ResumableSub
    Dim out As OutputStream = File.OpenOutput(Starter.Provider.SharedFolder, "tt.apk", False)
    out.WriteBytes(data, 0, data.Length)
    out.Close
    Return 0   '<---this is 0 
End Sub
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
Not sure

I turn off wifi and test this in the Elevator bad connection :) I download file 5MB
the ProgressDialog only blink can it be so fast ?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
the ProgressDialog only blink can it be so fast ?
You can time the download to see how many millisecs:
B4X:
If Result = DialogResponse.POSITIVE Then
        Dim StartTime As Long = DateTime.Now
        ProgressDialogShow("Download")
        Wait For (saveAPK(b)) Complete (Unused As Int)
        ProgressDialogHide
        Log(DateTime.Now-StartTime)
    End If
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
I add StartTime to Log and delete the file and check the size

>> connect to the internet
File deleted
File Exist
Bytes: 5,240
Download time: 48ms

>> add phone to Flight mode
File deleted
File Exist
Bytes: 5,240
Download time: 59ms


B4X:
Public Sub saveAPK(data() As Byte) As ResumableSub
    
    File.Delete(Starter.Provider.SharedFolder, "tt.apk")
    
    If File.Exists(Starter.Provider.SharedFolder, "tt.apk")  = False Then
        Log("File deleted ")
    End If
    
    Dim out As OutputStream = File.OpenOutput(Starter.Provider.SharedFolder, "tt.apk", False)
    out.WriteBytes(data, 0, data.Length)
    out.Flush
    out.Close

    If File.Exists(Starter.Provider.SharedFolder, "tt.apk")  = True Then
        Log("File Exist")
        Private FileSize As Int
        FileSize = File.Size(Starter.Provider.SharedFolder, "tt.apk")   
        Log("Bytes: " & NumberFormat(FileSize / 1024, 0, 0))
    
    End If
    Return 0
End Sub
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
Did search the forum and did see OutputStream,, File.WriteBytes is somthing I did miss

I am try to find out when I am finish to write the apk file
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
thanks

do I need to use wait ? , I will install the apk file when I am finish to save the file so it don't start install to soon
 
Upvote 0
Top