重启后的Android AlarmManager

编程入门 行业动态 更新时间:2024-10-28 11:24:50
本文介绍了重启后的Android AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一组需要在重启后保留的警报.我试过在引导接收器上使用,但它们不会再次启动.我不确定我是否理解引导接收器以及如何重新启动所有警报.我已经有一个接收器来接收通知,但不知道我是否可以使用同一个接收器,或者我是否需要一个新的接收器?

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 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

本文发布于:2023-11-27 04:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重启   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!