Android Question Services questions

Discussion in 'Android Questions' started by Albi, Feb 24, 2015.

  1. Albi

    Albi Active Member Licensed User


    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.)

    If StartingIntent.Action <> "android.intent.action.BOOT_COMPLETED" Then
    ToastMessageShow(quoteList.get(quoteNumber), False)
            noti.Light = 
            noti.Vibrate = 
            noti.OnGoingEvent = 
            noti.Sound = 
            noti.Icon = 
    "quote", quoteList.get(quoteNumber), Main)
            quoteNumber = quoteNumber + 
    End If

    Dim p As Period
    'p.Days = 1
        Do While AlarmTime < DateTime.Now 'make sure new alarmTime is in the future
            AlarmTime = DateUtils.AddPeriod(AlarmTime, p)

    "quoteNumber", quoteNumber)
    "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
    Sub Service_Create
    'loads quotes
    If File.Exists(File.DirInternal, "Init.txt"Then
            savedData = 
    If AlarmTime = 0 Then AlarmTime = DateTime.Now
            quoteNumber = savedData.Get(
    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.


    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!
  2. Erel

    Erel Administrator Staff Member Licensed User

    You should change the code to only show a notification if DateTime.Now + 2 * DateTime.TicksPerMinute > AlarmTime.

    1. There is no reason to call StopService inside Service_Destory.
    2. The old one is overwritten.
  3. Albi

    Albi Active Member Licensed 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?
  4. Albi

    Albi Active Member Licensed 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!!
  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