在整点开始服务,然后每小时重复一次
我希望在每小时开始时启动一个 IntentService,并在整点重复(下午 1:00、下午 2:00、下午 3:00 等)。
查看文档后,我相信使用 AlarmManager 来执行此操作是最佳实践。
我有一个 BootReceiver,它将首次启动应用程序,并在我的 onHandleIntent 中在重复任务后设置下一个警报(例如下面的振动)。我的问题是我无法将闹钟设置为在下一个小时前响起,我该怎么办?
为了进行测试,我在下面的代码中每 3 分钟发出一次警报
public class TimingAndControl extends IntentService
{ public TimingAndControl()
{
super(TimingAndControl.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent)
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "VibrateTag");
wakeLock.acquire();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(300);
NextAlarm();
}
private void NextAlarm()
{
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// The update frequency should often be user configurable. This is not.
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 3 * (60 * 1000);
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
if (nextUpdateTime.hour < 6 || nextUpdateTime.hour >= 18)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}
@Override
public void onCreate(){
Toast.makeText(this, "service starting", (10 * 1000)).show();
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(this, "service stopping", (10 * 1000)).show();
super.onDestroy();
}
}
I'm looking to start a IntentService at the start of every hour repeating exactly on the hour (1:00PM, 2:00PM, 3:00PM, etc.).
Having looked at the documentation, I believe using AlarmManager to do this is the best practice.
I have a BootReceiver which will start the application for the first instance and below in my onHandleIntent the next alarm is set after the task has been repeated(For example vibration below). My problem is that I am unable to set the alarm to go off at the top of the next hour, how would I go about this?
For testing I have the alarm going off every 3 minutes in the code below
public class TimingAndControl extends IntentService
{ public TimingAndControl()
{
super(TimingAndControl.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent)
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "VibrateTag");
wakeLock.acquire();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(300);
NextAlarm();
}
private void NextAlarm()
{
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// The update frequency should often be user configurable. This is not.
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 3 * (60 * 1000);
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
if (nextUpdateTime.hour < 6 || nextUpdateTime.hour >= 18)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}
@Override
public void onCreate(){
Toast.makeText(this, "service starting", (10 * 1000)).show();
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(this, "service stopping", (10 * 1000)).show();
super.onDestroy();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过 RTC_WAKEUP ,RTC 标志仅在设备从睡眠状态唤醒后才会触发警报。
Have you tried RTC_WAKEUP , RTC flag will trigger alarm only after the device wakes from Sleep .
您可以使用这样的代码找到下一小时的 UTC 毫秒时间(未测试)
然后您可以使用 AlarmManager 的 setReapting..() 方法之一,其时间类型接受 UTC 时间和 INTERVAL_HOUR 间隔。如果您发送的意图是固定的,多次调用它并没有什么坏处,因为每个 setRepeating() 都会删除以前的意图。
You can find the UTC millisecond time of next hour with code like this (not tested)
Then you can use one of the setReapting..() method of AlarmManager with time type that accepts UTC time and a INTERVAL_HOUR interval. If the intent you are sending is fixed, not harm in calling it multiple time as each setRepeating() will delete the previous ones.
计算下一小时的最高点时间,然后将其用作
AlarmManager
上setRepeating()
的第二个参数。Calculate when the top of the next hour is, then use that as the second parameter to
setRepeating()
onAlarmManager
.