Trying to add progress bars to the notification without libraries

jdiperla

Member
Licensed User
Longtime User
Just as the title says. I want to add a custom notification with a progress bar to my app. I have tried 2 or 3 libraries (Two of them might have been the same one, not sure) and I used the examples that some provided. I also followed the instructions (Android 15 Jar, etc..), Yet, when I try and use it, the whole thing just crashes on my device. Besides that, I want to be able to have that notification work on devices earlier than 4.0.3.

Any suggestions for me on this?
 

jdiperla

Member
Licensed User
Longtime User
:sign0163: :sign0085:

I am really having trouble with the libraries. I keep going back to them, but I just cant get it going.
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Well, after doing a looooong search for an answer, I still came up short, but I found this:

http://www.b4x.com/forum/additional...m-notification-library-barx-8.html#post133274

Basically I am using httputils2. When the Download starts, it starts the notification. When its done, it goes to JobDone and I want to set the notification to say something different. I even want it to update the progress bar.

I put all the download procedures into a Service so that it can do all this, but I get the error mentioned in the post above.

First off, I added these to the HTTPJOB class:

B4X:
Sub GetInputDir As String
Dim In As String
In = HttpUtils2Service.TempFolder
Return In
End Sub
Sub GetInputfile As String
Return taskId
End Sub

In my Activity, this is how I call the service to download the file. The service is called CoreService by the way:

B4X:
Sub DownloadTheFile()
CoreService.URLSource = imgurl
   CoreService.UrlTarget = newfiledir
   CoreService.URLFile = Main.curfile
    StartService(CoreService)

Here is my CoreService Service file code:

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim httputil As HttpJob

HttpUtils2Service.progressSub = "downloadProgress"
HttpUtils2Service.timerInterval = 200
   Dim DownloadCount As Int
   Dim UrlTarget As String
   Dim URLSource As String
   Dim URLFile As String
   Dim JobStatus As String
   Dim GJob As HttpJob
   
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
JobStatus = "STATUS_WORKING"
Dim CN As CustomNotification
DoNotif
Initiate
End Sub

Sub Service_Destroy

End Sub
Sub startinstall(targetdir As String, targetfile As String)

   
   
   
   ToastMessageShow("Decompressing Downloaded file", True)
   
   Dim arc As Archiver
Dim m As Map
Try

' arc.aSyncUnZip(targetdir, targetfile, targetdir, "arc")
 arc.aSyncUnZip(GJob.GetInputDir, GJob.GetInputfile, targetdir, "arc")

Catch
    Log(LastException.Message)
   ToastMessageShow("There was a problem decompressing the file", True)
End Try
End Sub
Sub arc_UnZipDone(completedWithoutError As Boolean,nbOfFiles As Int)
If completedWithoutError Then
    'File.Delete(File.DirRootExterna, Main.curfile)
   File.Delete(GJob.GetInputDir, GJob.GetInputfile)
   ToastMessageShow("Extraction completed successfully",True)
   JobStatus = "STATUS_DONE"
Else
ToastMessageShow("Extraction failed. Try again. Make sure you have enough space on your storage device", True)
JobStatus = "STATUS_DONE"
End If

End Sub
Sub DoNotif()
Dim CN As CustomNotification
CN.Initialize(3)

    CN.Number = 1
    CN.setIcon("icon")
    CN.TickerText = "Downloading File"
            CN.AutoCancel = False

            CN.SetTextColor("title", Colors.Gray)
    CN.SetText("title",  "Downloading: " & Main.curfile)
            CN.SetTextSize("title", 10)
            CN.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      CN.SetProgress(1, 100, 0, False)

    CN.Notify(1)
End Sub
Sub JobDone(Job As HttpJob)
Dim CN As CustomNotification
   Select Job.JobName
      Case "gDL"
         
   If GJob.Success = False Then
         ToastMessageShow("Error downloading the file.", True)
      CN.Number = 1
        CN.TickerText = "Game Download Complete"
            CN.AutoCancel = True

            
    CN.SetText("title",  Main.curfile & " had a problem downloading")
            CN.SetTextSize("title", 10)
            CN.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      CN.SetProgress(1, 100, 100, False)

    CN.Notify(1)
            Return
   End If
   If GJob.Success = True Then
   JobStatus = "STATUS_DONE"
CN.Initialize(3)
   CN.Number = 1
        CN.TickerText = "File Download Complete"
            CN.AutoCancel = True

            
    CN.SetText("title",  Main.curfile & " has downloaded successfully")
            CN.SetTextSize("title", 10)
            CN.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      CN.SetProgress(1, 100, 100, False)

    CN.Notify(1)
   startinstall(UrlTarget, URLFile)
   
   
   File.Copy(GJob.GetInputDir, GJob.GetInputfile, UrlTarget, URLFile)
   
   
   startinstall(UrlTarget, URLFile)
   End If
   Return
   End Select
   
   Job.Release
   
   
End Sub
Sub Initiate ()

GJob.Initialize("gDL", Me)
    GJob.Download(URLSource)
    
    End Sub
    Sub downloadProgress(tmp As ProgressStatus)
    Dim progress As Int
 Dim CN As CustomNotification
 progress=Round((tmp.downloaded*100)/tmp.total)
 CN.SetProgress(1,100,progress,False)
End Sub

I have tried several different ways of doing this. I just want a simple way of using all this to update my Notification and the progress bar. Also, if I do not comment out the following two lines in my service, my app just stops working:

B4X:
HttpUtils2Service.progressSub = "downloadProgress"
HttpUtils2Service.timerInterval = 200

It doesn't give me an error message, no logs or even any debug error messages. It just gives a message box saying that the app stopped working. Any thoughts on all of this?
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Couldn't figure it out using that. I tried something different and I think I am getting closer...


In httputils2service, I changed

B4X:
Public contentLength As Long

to

B4X:
Dim contentLength As Long

I then changed the coreservice using a timer. I think the issue now lies in the timer itself with my Do While Loop:

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim DownloadCount As Int
   Dim UrlTarget As String
   Dim URLSource As String
   Dim URLFile As String
   Dim JobStatus As String
   Dim GJob As HttpJob
   Dim tmr As Timer
   Dim timerint As Int
   Dim downloadedlength As Long
   Dim contentlength As Long
   
   
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
JobStatus = "STATUS_WORKING"
Dim cn As CustomNotification
DoNotif
Initiate
End Sub

Sub Service_Destroy

End Sub
Sub startinstall(targetdir As String, targetfile As String)
JobStatus = "STATUS_NONE"
   
   
   
   ToastMessageShow("Installing your game", True)
   
   Dim arc As Archiver
Dim m As Map
Try

' arc.aSyncUnZip(targetdir, targetfile, targetdir, "arc")
 arc.aSyncUnZip(GJob.GetInputDir, GJob.GetInputfile, targetdir, "arc")

Catch
    Log(LastException.Message)
   ToastMessageShow("There was a problem extracting the file", True)
End Try
End Sub
Sub arc_UnZipDone(completedWithoutError As Boolean,nbOfFiles As Int)
If completedWithoutError Then
    'File.Delete(File.DirRootExternal, Main.curfile)
   File.Delete(GJob.GetInputDir, GJob.GetInputfile)
   ToastMessageShow("Extraction completed successfully",True)
   JobStatus = "STATUS_DONE"
Else
ToastMessageShow("Extraction failed. Try again. Make sure you have enough space on your storage device", True)
JobStatus = "STATUS_DONE"
End If

End Sub
Sub DoNotif()
Dim cn As CustomNotification
cn.Initialize(3)

    cn.Number = 1
    cn.setIcon("icon")
    cn.TickerText = "Downloading File"

         cn.AutoCancel = False

            cn.SetTextColor("title", Colors.Gray)
    cn.SetText("title",  "Downloading: " & Main.curfile)
            cn.SetTextSize("title", 10)
            cn.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      cn.SetProgress(1, 100, 0, False)

    cn.Notify(1)
End Sub
Sub JobDone(Job As HttpJob)

   Select Job.JobName
      Case "gDL"
         
         
   If GJob.Success = False Then
         ToastMessageShow("Error downloading the file.", True)

            Return
   End If
   If GJob.Success = True Then
   
   startinstall(UrlTarget, URLFile)
   
   File.Copy(GJob.GetInputDir, GJob.GetInputfile, UrlTarget, URLFile)
   
   
   startinstall(UrlTarget, URLFile)
   End If
   Return
   End Select
   
   Job.Release
   
   
End Sub
Sub Initiate ()

'GJob.Initialize("gDL", Me)
'    GJob.download(URLSource)
tmr.Initialize("tmr", 200)
tmr.Interval = 200
tmr.Enabled = True
    
    End Sub
    Sub downloadProgress(tmp As ProgressStatus)
    Dim progress As Int

 progress=Round((tmp.downloaded*100)/tmp.total)
 'cn.SetProgress(1,100,progress,False)
End Sub

Sub tmr_tick()
 
Dim h As HttpResponse
contentlength = HttpUtils2Service.contentLength
Dim cn As CustomNotification
cn.Initialize(3)
cn.AlertOnce = True
cn.Insistent = False

    cn.Number = 1
    cn.setIcon("icon")
    cn.TickerText = "Downloading File"
            cn.AutoCancel = False

            cn.SetTextColor("title", Colors.Gray)
    cn.SetText("title",  "Downloading: " & Main.curfile)
            cn.SetTextSize("title", 10)
            cn.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      cn.SetProgress(1, 100, 0, False)

    cn.Notify(1)
   GJob.Initialize("gDL", Me)
   GJob.download(URLSource)
    
If    File.Exists(GJob.GetInputDir, GJob.GetInputfile)=True Then 
      downloadedlength=   File.Size(GJob.GetInputDir, GJob.GetInputfile)
   End If
   Dim tmp As ProgressStatus
   tmp.Downloaded=downloadedlength
   tmp.Total=contentlength
       Dim progress As Int

 progress=Round((tmp.downloaded*100)/tmp.total)

 Do While progress < 100
 
 cn.SetProgress(1,100,progress,False)
 cn.Notify(1)
 'DoEvents
 Loop
 
 If progress = 100 Then
JobStatus = "STATUS_DONE"
cn.Initialize(3)
   cn.Number = 1
        cn.TickerText = "Download Complete"
            cn.AutoCancel = True

            
    cn.SetText("title",  Main.curfile & " has downloaded successfully")
            cn.SetTextSize("title", 10)
            cn.SetImage("image", LoadBitmap(File.DirAssets, "menu_info.png"))
      cn.SetProgress(1, 100, 100, False)

    cn.Notify(1)
tmr.Enabled = False
    End If
   
End Sub

It seems that it loops but its stuck in the loop and keeps refreshing the notification but not the progress bar. Suggestions on this piece of code?
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
OK, so I removed the loop and the app should be checking every few ticks. I think the problem is find the size of the file I am downloading as the progress bar is not updating nor is it performing the actions its supposed to perform after reaching %100. It seems as if its still stuck in a loop regardless of not having a loop.

Anyone have a snippet I could use for this problem?
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Ok, so I fixed the whole thing by using a callsub function. However, I realized after doing lots of tests, that its not my code that was the problem with updating the progress bar (Atleast I do not think), but rather it is something with the progressbar layout. I dont understand what it is thats causing the issue, but I tried setting the title of the Notification to the percentage that was downloaded. While the title kept up with the progress, the progress bar didn't move at all.

Is this a known bug? Or am I just THAT stupid?
 
Upvote 0
Top