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

Discussion in 'Android Questions' started by asales, May 28, 2019.

  1. asales

    asales Well-Known Member Licensed 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.

    Code:
    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/6017+7/6017+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
     
    Kope likes this.
  2. DonManfred

    DonManfred Expert Licensed User

    How are downloading a quote and restarting a service related?

    Move the downloads to the starter service. NO NEED to stop/restart anything.
     
    Erel likes this.
  3. asales

    asales Well-Known Member Licensed User

    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: May 28, 2019
  4. asales

    asales Well-Known Member Licensed 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:
    Code:
    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/6013+30/6018+30/60))
        
          
    Log($"Next time is: $Time{t}"$
          
    StartServiceAt(Me,t,True)
        
    StopService("")
    End Sub
     
  5. Kope

    Kope 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
     
    ShaneG30 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice