Android Question [Solved] How to download quote and show a notification in schedule time service?

asales

Expert
Licensed User
Longtime User
I use the code from this post to schedule time and show a notification. Works fine.
https://www.b4x.com/android/forum/threads/b4x-calculating-the-next-scheduled-time.65041/#content

Now I want to download a quote
https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content
and show the text in the notification.

I tried to put the code to download code in service start, before the notification, to test, but I get a problem: the notification restarted over and over.

How I can download the quote in the service and show it in the notification?

Thanks in advance for any tip.

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("shedule.service_start()")
    Log("i´m started. So i´ll do my job NOW and reshedule me for the next time")
    
'    DownloadQuote  'RESTART THE NOTIFICATION SEVERAL TIMES
    
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.AutoCancel = True
    n.SetInfo(Application.LabelName, "Quote of the day", Main)
    n.Notify(1)
    
    Dim t As Long
    t = FindNextTime(Array As Double(17+6/60, 17+7/60, 17+8/60))
    
    Log($"Next time is: $Time{t}"$) 
    StartServiceAt(Me,t,True)
    StopService("")
End Sub

Sub DownloadQuote
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim quotes As List = jp.NextArray
        For Each quot As Map In quotes
            Log("Title: " & quot.Get("title"))
            Log("Content: " & quot.Get("content"))
        Next
    End If
    j.Release
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
How are downloading a quote and restarting a service related?

Move the downloads to the starter service. NO NEED to stop/restart anything.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
How are downloading a quote and restarting a service related?

Move the downloads to the starter service. NO NEED to stop/restart anything.

The code above is only for tests.
I have a schedule service that starts 3 times in a day (9am, 1pm and 6pm) and show a notification.
I want to download a random quote and show it in the notification.
If I put the download in the Starter service, the notification will not show the random quote. I don't know how to solve this problem.
 
Last edited:
Upvote 0

asales

Expert
Licensed User
Longtime User
I think I figured out:
1 - Created a new service: ServiceJobs
2 - Move the DownloadQuote and Notification subs to the ServiceJobs
3 - Call the subs in ServiceJobs when the ScheduleTime service starts:
B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("shedule.service_start()")
    Log("i´m started. So i´ll do my job NOW and reshedule me for the next time")
    
    CallSubDelayed(ServiceJobs, "DownloadQuote")
    
    Dim t As Long
    t = FindNextTime(Array As Double(9+30/60, 13+30/60, 18+30/60))
    
      Log($"Next time is: $Time{t}"$) 
      StartServiceAt(Me,t,True)
    StopService("")
End Sub
 
Upvote 0

Kope

Active Member
Licensed User
I have been planning to work on a similar project but finding it difficult with how to understand service. Will be grateful if you can upload a small project (zip)

Thank You
 
Upvote 0
Top