我可以在Android中设置AlarmManager的结束时间吗? [英] Can I set an end time for AlarmManager in Android?

查看:114
本文介绍了我可以在Android中设置AlarmManager的结束时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置 AlarmManager 在特定时间关闭,然后按间隔重复。是否可以在停止多少个间隔后告诉它?或者甚至在什么时候停止?

I have set up an AlarmManager to go off at a specific time, and then repeat in intervals. Is it possible to tell it after how many intervals to stop? Or even at what time to stop?

这是我到目前为止所做的:

This is what I have so far:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 56);
    Intent intent = new Intent(this, MyService.class);
    alarmIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60 * 60, alarmIntent);

我是否还需要做任何事情以确保它每天都会消失?或者它已经在做了吗?

Also do I need to do anything to ensure it will go off every day? Or is it already doing that?

推荐答案


是否有可能在多少个间隔之后告诉它停?或者甚至在什么时候停止?

Is it possible to tell it after how many intervals to stop? Or even at what time to stop?

没有办法为AlarmManager指定结束时间,但你可以做的是,每当您在广播接收器中收到待处理的意图时,请检查是否需要取消该警报。要取消警报,您需要调用alarmManager.cancel(pendingIntent),其中pendingIntent具有与您最初使用的PendingIntent相同的requestCode。

There's no way to specify an end time for an AlarmManager, but what you can do is, every time you receive your pending intent in your broadcast receiver, check to see if you need to cancel that alarm. To cancel the alarm, you need to call alarmManager.cancel(pendingIntent), where pendingIntent has the same requestCode that you used for the PendingIntent that you originally created the alarm with.


我是否还需要做任何事情以确保它每天都会消失?

Also do i need to do anything to ensure it will go off every day?

你需要将间隔设置为:1000 * 60 * 60 * 24如果您希望每天触发一次。

You need to set the interval to: 1000 * 60 * 60 * 24 if you want it to fire once per day.

您还需要注意设备重启的情况。如果用户重新启动设备,则将清除警报,您需要重新创建它们。您需要实现另一个侦听RECEIVE_BOOT_COMPLETED的BroadcastReceiver,并在onReceive()中添加逻辑以重新创建警报。

You also need to take care of the situation of a device reboot. If the user reboots the device, the alarms will be cleared and you need to recreate them. You will need to implement another BroadcastReceiver that listens for RECEIVE_BOOT_COMPLETED, and add logic in your onReceive() to recreate your alarms.

此外,此行是不必要的:

Also, this line is unnecessary:

calendar.setTimeInMillis(System.currentTimeMillis());

此行:

Calendar calendar = Calendar.getInstance();

已经为您提供了一个设置为您创建它的确切时间的Calendar对象。

already gives you a Calendar object set to the exact time you create it.

您还应该添加以下行,以确保您的闹钟每天正好在13:56点火:

You should also add the following lines to make sure your alarm fires at exactly 13:56 each day:

calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

这篇关于我可以在Android中设置AlarmManager的结束时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆