Android将闹钟设置为一天中的特定时间 [英] Android set repeating alarm for certain time of day
问题描述
我正在尝试设置一个重复警报,该警报将每分钟但仅在8:00和22:00之间下载文件.我觉得我真的很接近,但是看不到我正在犯的错误.当前,广播接收器未激活.如果将重复警报手动设置为 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+ 10000,60000,pendingIntent); ,它将正常运行.任何指导将不胜感激.
I am trying to set a repeating alarm that will will download a file every minute but only between 8:00 and 22:00. I feel like I'm really close but I can't see the error I'm making. Currently the broadcast receiver is not activating. If set the repeating alarm manually to alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, 60000,pendingIntent); it works fine. Any guidance would be much appreciated.
protected void scheduleNextUpdate()
{
Intent intent = new Intent("TEST");
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int updateInterval = 1;
long nextUpdate =(60000 * updateInterval);
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + nextUpdate;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
if (nextUpdateTime.hour < 8 || nextUpdateTime.hour > 22)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, nextUpdateTimeMillis,pendingIntent);
boolean alarmUp = (PendingIntent.getBroadcast(this, 0,
new Intent("TEST"),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
}
}
推荐答案
您应将警报设置为在8:00至22:00之间重新发送,就像您提到的那样:
You should set your alarm reapeating when it is between 8:00 and 22:00 like you mentioned:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, 60000,pendingIntent);
这将每分钟重复一次.但是您必须明确取消警报.您可以在下载完成后取消它,方法是检查它是否已经是22:00.或在22:00时触发的另一个警报.否则它不会停止.
This will repeat every minute. But you must explicitly cancel the alarm. You can cancel it after download is completed, by checking if its already 22:00. Or by another alarm that will be triggered when it is 22:00. Otherwise it will not stop.
使用alarmManager.cancel (pendingIntent)
希望这会有所帮助.
这篇关于Android将闹钟设置为一天中的特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!