停止在特定时间的服务

编程入门 行业动态 更新时间:2024-10-26 08:27:55
本文介绍了停止在特定时间的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我多次使用下面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个选项:

  • 每次报警触发器(即每1分钟),检查时间,取消的话时间是下午6点以后。
  • 设置在AlarmManager一个一次性报警,下午6点正好熄灭。在这个报警,请取消。
  • 我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:

  • Each time the alarm triggers (i.e. every 1 minute), check the time, and cancel if time is after 6PM.
  • Set a once-off alarm in AlarmManager to go off at 6PM exactly. In that alarm, cancel.
  • 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);

    更多推荐

    停止在特定时间的服务

    本文发布于:2023-11-27 10:06:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637630.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:时间   在特定

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!