Android Question sending message to waiting queue issue

German Buchmuller

Member
Licensed User
Longtime User
Hi, Ive got an httpserver with images and a text file. My app has a service runing on the background every 30s. It downloads a textfile from my http server, and if it has changed from the previus one, then it pop a notification with the new text. In other words, when I update the content on my text file in my server, my app will show a notification with the updated text.
This is my service code:

B4X:
#Region  Service Attributes
    #StartAtBoot: true
#End Region

Sub Process_Globals
    Private logo As Bitmap
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

    StartServiceAtExact(Me, DateTime.Now + 30 * 1000, True)
    If File.Exists(File.DirInternal, "promo7.txt")=False Then
        Dim TextWriter1 As TextWriter
        TextWriter1.Initialize(File.OpenOutput(File.DirInternal, "promo7.txt", True))
        TextWriter1.Write("holaa")
        TextWriter1.Close
    End If
    logo = LoadBitmapResize(File.DirAssets, "icon.png", 24dip, 24dip, False)
   
   
    If Starter.descargando=False Then
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://g3dsoftware.ddns.net:8080/Tenti/promo1.txt")
End If
End Sub
Sub JobDone(job As HttpJob)
    If job.Success Then

        Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.txt", False)
        File.Copy2(job.GetInputStream, out)
        out.Close '<------ very important
        If File.ReadString(File.DirInternal,"promo7.txt")<> File.ReadString(File.DirRootExternal, "filename.txt") Then
            File.Delete(File.DirInternal,"promo7.txt")
            Dim TextWriter1 As TextWriter
            TextWriter1.Initialize(File.OpenOutput(File.DirInternal, "promo7.txt", True))
            TextWriter1.Write(File.ReadString(File.DirRootExternal,"filename.txt"))
            TextWriter1.Close
            Dim n As NB6
            n.Initialize("default", Application.LabelName, "HIGH").AutoCancel(True).SmallIcon(logo)
           
            n.Color(Colors.RGB(245,149,66))
            n.Build("¡Promo!",File.ReadString(File.DirRootExternal,"filename.txt"), "tag1", Main).Notify(4) 'It will be Main (or any other activity) instead of Me if called from a service.
        End If
       
       
       
    Else
        Log("Error: " & job.ErrorMessage)
    End If
    job.Release
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy

End Sub

The problem with my app is that my Main Activity has an imageview, which bitmap is also downloaded from my http server by the service ImageDownloader. Sometimes when I start my app, ImageDownloader doesnt download the image from my http server. In the log I get
B4X:
sending message to waiting queue (CallSubDelayed - Download)
My Main activity code is:
B4X:
Sub Globals
    Private temp As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
   
    If FirstTime Then
       
        Dim links As Map
        links.Initialize
        links.Put(temp, "http://g3dsoftware.ddns.net:8080/Tenti/inicio1.jpg")
        CallSubDelayed2(ImageDownloader, "Download", links)
   
    End If
   
End Sub


Sub error
        ToastMessageShow("Error al conectar con el servidor",True)
   
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    CallSub(ImageDownloader, "ActivityIsPaused")
End Sub

Sub done1
       ToastMessageShow("Exito al cargar",True)
   
End Sub
My ImageDownloader code is as follows:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private cache As Map
    Private tasks As Map
    Private ongoingTasks As Map

   
End Sub

Sub Service_Create
    tasks.Initialize
    cache.Initialize
    ongoingTasks.Initialize
    cache.Clear
   
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub Download (ImageViewsMap As Map)
    For i = 0 To ImageViewsMap.Size - 1
        tasks.Put(ImageViewsMap.GetKeyAt(i), ImageViewsMap.GetValueAt(i))
        Dim link As String = ImageViewsMap.GetValueAt(i)
        If cache.ContainsKey(link) Then
            Dim iv As ImageView = ImageViewsMap.GetKeyAt(i)
            iv.SetBackgroundImage(cache.Get(link))
        Else If ongoingTasks.ContainsKey(link) = False Then
            ongoingTasks.Put(link, "")
            Dim j As HttpJob
            j.Initialize(link, Me)
            j.Download(link)
        End If
    Next
End Sub

Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As Bitmap = Job.GetBitmap
        cache.Put(Job.JobName, bmp)
        If tasks.IsInitialized Then
            For i = 0 To tasks.Size - 1
                Dim link As String = tasks.GetValueAt(i)
                If link = Job.JobName Then
                    Dim iv As ImageView = tasks.GetKeyAt(i)
                    iv.SetBackgroundImage(bmp)
                End If
            Next
            If Job.JobName="http://g3dsoftware.ddns.net:8080/Tenti/inicio1.jpg" Then
                CallSub(Main,"done1")
            End If
            If Job.JobName="http://g3dsoftware.ddns.net:8080/Tenti/inicio2.jpg" Then
                CallSub(Main,"done2")
            End If
            If Job.JobName="http://g3dsoftware.ddns.net:8080/Tenti/inicio3.jpg" Then
                CallSub(Main,"done3")
            End If
        End If
    Else
        If Job.JobName="http://g3dsoftware.ddns.net:8080/Tenti/inicio1.jpg" Then
            CallSub(Main,"error")
        End If
       
       
   
    End If
    Job.Release
End Sub
Sub ActivityIsPaused
    tasks.Clear
   
End Sub

My final question is: Where is the error that makes my ImageDownloader dont work all the time? Thanks to any reply
 

KMatle

Expert
Licensed User
Longtime User
This message occurs if the activity isn't in the foreground (= user doesn't see it) and you try to update a view or so . You can start the activity (which is a bad user experience). Better: Show a notification like "new data" or similar.
 
Upvote 0

German Buchmuller

Member
Licensed User
Longtime User
This message occurs if the activity isn't in the foreground (= user doesn't see it) and you try to update a view or so . You can start the activity (which is a bad user experience). Better: Show a notification like "new data" or similar.
Thanks for the reply. I didnt understand well what should I do. My notifications must update as I change a txt file on my http server, just as the notification service code says. So then, what should I do for my ImageDownloader to properly work? Thanks again
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several mistakes / bad practices in the code: JobDone sub instead of Wait For, accessing File.DirRootExternal, using TextWriter instead of File.WriteString, scheduling the service to start every 30 seconds with StartServiceAt (it will not work on new devices).

If you want your app to run all the time: Background location tracking

This message means that the service wasn't able to start:

sending message to waiting queue (CallSubDelayed - Download)

I recommend you to remove this service. You don't need it. It is very simple to download an image. Either download the image directly in the activity (in Activity_Resume) or download it from the starter service. The starter service is never paused.
 
Upvote 0

German Buchmuller

Member
Licensed User
Longtime User
Thanks Erel! Sure I will use your advice!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…