重启后的Android AlarmManager [英] Android AlarmManager after reboot
问题描述
我有一组需要在重启后保留的警报.我试过在引导接收器上使用,但它们不会再次启动.我不确定我是否理解引导接收器以及如何重新启动所有警报.我已经有一个接收器来接收通知,但不知道我是否可以使用同一个接收器,或者我是否需要一个新的接收器?
I have a set of alarms that I need to keep after reboot. I've tried using on an boot receiver but they won't start again. I'm not sure if I understand the boot receiver and how to then restart all the alarms. I already have one receiver for my notifications, but don't know whether I can use the same receiver or if I need a new one?
谁能告诉我任何好的教程或帮助我?
Could anyone point me to any good tutorials or help me out?
干杯
代码:
DatabaseHandler db = new DatabaseHandler(this);
List<UAlarm> alarms = db.getAllAlarms();
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
for (UAlarm ua : alarms) {
String programme = ua.getTitle();
String startTime = ua.getStart();
String endTime = ua.getEnd();
String nowPlaying = ua.getChannel();
db.addAlarm(new UAlarm(programme, startTime, endTime, nowPlaying, ""));
final UAlarm ut = new UAlarm();
ut.setTitle(programme);
ut.setStart(startTime);
ut.setEnd(endTime);
ut.setChannel(nowPlaying);
ut.setId(db.getLastEntered());
String [] bla = startTime.split(":");
int hour = Integer.parseInt(bla[0].trim());
int minute = Integer.parseInt(bla[1].trim());
minute -= 2;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
Intent intenta = new Intent(this, NotificationMenu.class);
String name = programme;
intenta.putExtra("name", name);
intenta.putExtra("id", db.getLastEntered());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ua.getId(),
intenta, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pendingIntent);
}
}
NotificationMenu 是通知,这就是我使用 AlarmManager 的原因
with NotificationMenu being the notifications, which is why I'm using the AlarmManager
推荐答案
我不确定我是否理解引导接收器以及如何重新启动所有警报.
I'm not sure if I understand the boot receiver and how to then restart all the alarms.
只需调用您的代码即可在 AlarmManager
上调用 setRepeating()
(或其他).
Just call your code to call setRepeating()
(or whatever) on AlarmManager
.
例如,在这个示例项目中,PollReceiver
设置为接收 BOOT_COMPLETED
.在 onReceive()
中,它重新安排警报:
For example, in this sample project, PollReceiver
is set to receive BOOT_COMPLETED
. In onReceive()
, it reschedules the alarms:
package com.commonsware.android.schedsvc;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD=5000;
@Override
public void onReceive(Context ctxt, Intent i) {
scheduleAlarms(ctxt);
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, ScheduledService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
}
}
这篇关于重启后的Android AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!