Android Question StartServiceAt headaches...

Creaky

Member
Licensed User
Longtime User
Ok I'm going nuts here :)

I've tried a couple of solutions for starting a service every day at the same time, but I keep running into loops!
Can somebody _please_ post the complete code for a service that i.e. runs every day at 17:00 hours and writes something to the log?

I'm absolutely sure I'm doing something wrong, but I can't figure out what and I need an example of how it should be done correctly
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess that the difficulty is to calculate the time. You can use this sub:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(DateUtils.TicksToString(NextTimeInstance(17, 0)))
End Sub

Sub NextTimeInstance (Hours As Int, Minutes As Int) As Long
   Dim today As Long = DateTime.Now
   today = DateUtils.SetDateAndTime(DateTime.GetYear(today), DateTime.GetMonth(today), _
     DateTime.GetDayOfMonth(today), Hours, Minutes, 0)
   If today < DateTime.Now Then
     Dim p As Period
     p.Days = 1
     Dim tomorrow As Long = DateUtils.AddPeriod(today, p)
     Return tomorrow
   Else
     Return today
   End If
End Sub
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Thank you Erel!

I tried your sub in my own service, but somehow I got strange loops where the service kept restarting itself indefinitely...
So that is why I'm looking for the code for a complete service, so that I can understand the whole framework of a correctly functioning service.
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
:D Quick as lightning.

This is my current Service_Start sub.
What I do now is restart the service an hour from Now and each time check if the current hour falls in the required timeframe.

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("Push service started.")
    Dim Now As Long
    objSettings.Initialize
    If File.Exists(File.DirInternal, "heaven.db") = False Then
        File.Copy(File.DirAssets, "heaven.db", File.DirInternal, "heaven.db")
    End If
    If Not(PushDatabase.IsInitialized) Then
        PushDatabase.Initialize(File.DirInternal, "heaven.db", False)
    End If

    Log("Reporting in. Notify for events? > " & objSettings.blnEvents)
    Log("Reporting in. Notify for next day events? > " & objSettings.blnImportantEvents)
    Log("Reporting in. Notify for all events? > " & objSettings.blnAllEvents)
    Now    = DateTime.GetHour(DateTime.Now)
   
    If objSettings.blnEvents AND objSettings.blnAllEvents Then
        If Now >= 17 AND Now < 19 Then
            checkDailyNotifications
        Else
            Log("Skipped all events notification check")
        End If
        If Now >= 22 AND Now < 24 Then
            checkNextDayNotifications
        Else
            Log("Skipped next day notification check")
        End If
    Else If objSettings.blnEvents AND objSettings.blnImportantEvents Then
        If Now >= 17 AND Now < 19 Then
            checkImportantNotifications
        Else
            Log("Skipped important events notification check")
        End If
        If Now >= 22 AND Now < 24 Then
            checkImportantNextDayNotifications
        Else
            Log("Skipped important next day notification check")
        End If
    End If

    If objSettings.blnNews Then
        getNews
    End If
   
    StartServiceAt("",DateTime.Now + 600000,True)
End Sub
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Ok I'll give that a try!
Can I schedule te service to run twice in one day?
Like this?

B4X:
Sub Service_Start (StartingIntent As Intent)
StartServiceAt(Me, NextTimeInstance(17, 0) ,True)
StartServiceAt(Me, NextTimeInstance(22, 0) ,True)
End Sub

Or will the second schedule override the first?
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Ok, so this should work then?

B4X:
    If Now = 17 Then
        StartServiceAt(Me, NextTimeInstance(22, 0) ,True)
    Else
        StartServiceAt(Me, NextTimeInstance(17, 0) ,True)
    End If
 
Upvote 0
Top