Android Question Services questions

Albi

Active Member
Licensed User
Longtime User
Hello,

I'm using StartServiceAt to generate an alarm with notification.

Service_Start with StartAtBoot = True

When I turn off the device during a time when the alarm would fire, when turning the device on, the alarm fires (because the time has passed so it should fire).

However, I do not want it to do so.

I want it so that when the alarm is set, it should go off once a day. I want it only to fire if/when that time occurs.
So using code found on the forum, I have changed the Service_Start module to this
(In the code below, I've changed it from firing every day to every 3 minutes so i can test it.)

B4X:
    If StartingIntent.Action <> "android.intent.action.BOOT_COMPLETED" Then
        ToastMessageShow(quoteList.get(quoteNumber), False)
        noti.Initialize
        noti.Light = False
        noti.Vibrate = True
        noti.OnGoingEvent = False
        noti.Sound = True
        noti.Icon = "icon"
        noti.SetInfo("quote", quoteList.get(quoteNumber), Main)
        noti.Notify(1)
        quoteNumber = quoteNumber + 1
    End If

    Dim p As Period
    'p.Days = 1
    p.Seconds = 180 'FOR TESTING ONLY - SO YOU DONT HAVE TO WAIT A DAY
    Do While AlarmTime < DateTime.Now 'make sure new alarmTime is in the future
        AlarmTime = DateUtils.AddPeriod(AlarmTime, p)
    Loop
    StartServiceAt("",AlarmTime,True)

    savedData.Put("quoteNumber", quoteNumber)
    savedData.Put("alarm1", AlarmTime)
    File.WriteMap(File.DirInternal, "Init.txt", savedData) 'save the map

I save the AlarmTime (and quote number) in a map so that it knows what these should be when it restarts.

This is loaded in the Service_Create module
B4X:
Sub Service_Create
    quoteList.Initialize
    Quotes 'loads quotes
   
    If File.Exists(File.DirInternal, "Init.txt") Then
        savedData = File.ReadMap(File.DirInternal,"Init.txt")
        If AlarmTime = 0 Then AlarmTime = DateTime.Now
        quoteNumber = savedData.Get("quoteNumber")
    Else
        savedData.Initialize
    End If
End Sub

The issue I found is:
When I turn on the phone after missing an alert, the toast message DOES NOT appear (great!) but the notification does. The notification is listed with the time that the phone was turned on. The text has been updated so it appears the quoteNumber+1 code is being run.

Service_Destroy

1. The tutorial says that StopService() calls this Sub. So why in the code examples is StopService("") inside the Service_Destroy Sub? Does StopService(alarmService) call that module's Service Destroy sub and then StopService("") stops the actual service?
2. If I set a new time for an alarm, does the old one get overwritten, or do I need to run CancelScheduledServices?

Many thanks for your help!
 

Albi

Active Member
Licensed User
Longtime User
great! thank you very much.

I don't quite understand the 2*DateTime.TicksPerMinute?
Are you just adding 120000 ticks to the comparison, and how does that work?
 
Upvote 0

Albi

Active Member
Licensed User
Longtime User
I changed it slightly to
AlarmTime + 2 * DateTime.TicksPerMinute > DateTime.Now
which i think means that as long as it fires within 2 minutes of the alarm then it will send a notification, otherwise it won't. This seems to have done the trick. Thanks very much for your help!!
 
Upvote 0
Top