android - Stop a service at specific time -


i starting service using below code repeatedly. service starts @ 8am everyday. , alarmmanager repeates @ every 1 min. want stop sevice @ 6pm. how can ?

    alarmmanager manager = (alarmmanager) getsystemservice(context.alarm_service);     pendingintent loggerintent = pendingintent.getbroadcast(this, 0,new intent(this,alarmreceiver.class), 0);      calendar timeoff9 = calendar.getinstance();     timeoff9.set(calendar.hour_of_day, 08);     timeoff9.set(calendar.minute, 00);     timeoff9.set(calendar.second, 00);     //--------------------------------------------------------------------------------------------------------------------     long duration = userinterval * 60 * 1000;     manager.setrepeating(alarmmanager.rtc_wakeup,timeoff9.gettimeinmillis(), duration, loggerintent); 

in order cancel @ 6pm exactly, consider 2 options:

  1. each time alarm triggers (i.e. every 1 minute), check time, , cancel if time after 6pm.
  2. set once-off alarm in alarmmanager go off @ 6pm exactly. in alarm, cancel.

i prefer option 2 simplicity, , modularity of each code block. me, use (2) in proof-of-concept code, while working on solution.

but option 1 better resources point of view, android system needs remember single alarm. use (1) in final production code.

this way of working personal preference, , ppl use (1) right away start.

the details cancelling below...


as how cancel alarm, don't beat answer @commonsware....

below answer copied how cancel repeating alarm?


call cancel() on alarmmanager equivalent pendingintent 1 used setrepeating():

intent intent = new intent(this, alarmreceive.class); pendingintent sender = pendingintent.getbroadcast(this,                0, intent, 0); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service);  alarmmanager.cancel(sender); 


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -