重启后重复报警管理器 [英] Repeating Alarm Manager After reboot
问题描述
我想用通知消息创建广播 AlarmManager(重复).我从 Pickers 传递我的日历对象.如果我不重新启动我的设备,它会正常工作.但是,当我重新启动设备时,如您所知,我的日历对象将为空.重新启动后如何管理重复的闹钟以及如何保留我的日历计划?谢谢你的想法.
i want to create broadcast AlarmManager(repeating) with Notification message.i pass my calender object from Pickers. If i don't reboot my device it works normally. However, when i reboot my device,as you know my calander object will be null. How can i manage my repeating alarm after rebooting and how can i hold my Calendar schedules? Thanks for your ideas.
public class MyReceiver extends BroadcastReceiver {
private static final int PERIOD = 10000;
final public static String ALARM_ID = "AlarmId";
final public static String NOTIFICATION_ID = "NotificationId";
@Override
public void onReceive(Context ctxt, Intent i) {
}
static void scheduleAlarms(Context ctxt,Calendar c) {
AlarmManager alarManager = (AlarmManager) ctxt
.getSystemService(Context.ALARM_SERVICE);
//notification servise
Intent i = new Intent(ctxt, ScheduledService.class);
i.putExtra(ALARM_ID, 1);
i.putExtra(NOTIFICATION_ID, 1);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),PERIOD, pi);
}
推荐答案
您需要使用 BroadcastReceiver
并将其设置为响应 BOOT_COMPLETED
消息.例如
You need to use a BroadcastReceiver
and set it to respond to BOOT_COMPLETED
messages. For example
在清单中注册您的 BroadcastReceiver
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
处理代码中的消息
MyBootReceiver.java
public class MyBootReceiver extends BroadcastReceiver
{
private static final String TAG = "MyBootReceiver";
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
Calendar cal = this.getMyCalendar();
this.scheduleAlarms(context, cal);
}
private Calendar getMyCalendar() {
// get your calendar object
}
private void scheduleAlarms(Context ctxt, Calendar c) {
AlarmManager alarManager = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
//notification servise
Intent i = new Intent(ctxt, ScheduledService.class);
i.putExtra(ALARM_ID, 1);
i.putExtra(NOTIFICATION_ID, 1);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),PERIOD, pi);
}
}
这将在启动时重置您的闹钟时间表.
This will reset your alarm schedule on boot.
这篇关于重启后重复报警管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!