Android Question Services questions

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

  1. Albi

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

    Code:
    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
    Code:
    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!
     
  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!!
     
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