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:
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
My Main activity code is:
My ImageDownloader code is as follows:
My final question is: Where is the error that makes my ImageDownloader dont work all the time? Thanks to any reply
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)
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
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