我多次使用下面code开始我的服务。我的服务启动每天早上8点在。和AlarmManager repeates每隔1分钟。我想在下午6时停止这种服务队。我该怎么做呢?
AlarmManager经理=(AlarmManager)getSystemService(Context.ALARM_SERVICE); 的PendingIntent loggerIntent = PendingIntent.getBroadcast(在此,0,新意图(此,AlarmReceiver.class),0); 日历timeOff9 = Calendar.getInstance(); timeOff9.set(Calendar.HOUR_OF_DAY,08); timeOff9.set(Calendar.MINUTE,00); timeOff9.set(Calendar.SECOND,00); //-------------------------------------------------------------------------------------------------------------------- 持续时间长= userinterval * 60 * 1000; manager.setRepeating(AlarmManager.RTC_WAKEUP,timeOff9.getTimeInMillis(),持续时间,loggerIntent);解决方案
为了下午6时取消准确地说,我会考虑2个选项:
我preFER选项2为简单起见,每个code座的模块化。所以对我来说,我会用(2)在我的验证的概念,code,而在解决方案的工作。
不过,选项1是从一个资源点更好,因为Android系统只需要记住一个报警。我会在我的最终生产code使用(1)。
这种工作方式只是我个人的preference,大多数脂肪酶可能会说,使用(1)从一开始的时候了。
关于取消详情如下...
至于如何取消报警,你不经常@commonsware打一个答案......
下面回答如何取消重复报警复制?
呼叫取消()在 AlarmManager 用等效的的PendingIntent 给您 setRepeating所使用的():
意向意图=新意图(这一点,AlarmReceive.class);发件人的PendingIntent = PendingIntent.getBroadcast(这一点, 0,意图,0);AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);alarmManager.cancel(寄件人);I am starting my service using below code repeatedly. My service starts at 8am everyday. And AlarmManager repeates at every 1 min. I want to stop this sevice at 6pm. how can I do this ?
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 to cancel at 6pm exactly, I would consider 2 options:
I prefer option 2 for simplicity, and modularity of each code block. So for me, I would use (2) in my proof-of-concept code, while working on the solution.
But option 1 is better from a resources point of view, as the Android system only needs to remember a single alarm. I would use (1) in my final production code.
This way of working is just my personal preference, and most ppl probably will say to use (1) right away from the start.
The details about cancelling are below...
As for how to cancel an alarm, you don't often beat an answer by @commonsware....
Below answer copied from How to cancel this repeating alarm?
Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with 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);
更多推荐
停止在特定时间的服务
发布评论