我有一组,我需要不断地重启后报警。我使用已经试过上启动接收器,但他们会不会重新开始。我不知道如果我理解引导接收器,以及如何重新启动所有报警。我已经有一个接收器,我的通知,但不知道我是否可以使用相同的接收器,或者如果我需要一个新的吗?
任何人都可以点我的任何好的教程或帮助我吗?
干杯
code:
数据库处理器DB =新的数据库处理器(本);
名单< UAlarm>报警= db.getAllAlarms();
AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
对于(UAlarm UA:报警){
字符串程序= ua.getTitle();
字符串的startTime = ua.getStart();
串endTime的= ua.getEnd();
串nowPlaying = ua.getChannel();
db.addAlarm(新UAlarm(计划的startTime,endTime的,nowPlaying,));
最后UAlarm UT =新UAlarm();
ut.setTitle(节目);
ut.setStart(startTime时);
ut.setEnd(endTime的);
ut.setChannel(nowPlaying);
ut.setId(db.getLastEntered());
的String [] BLA = startTime.split(:);
INT小时=的Integer.parseInt(BLA [0] .trim());
INT分钟=的Integer.parseInt(BLA [1] .trim());
分钟 - = 2;
日历CAL = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,小时);
cal.set(Calendar.MINUTE,分钟);
意图intenta =新的意图(这一点,NotificationMenu.class);
字符串名称=程序;
intenta.putExtra(姓名,名);
intenta.putExtra(ID,db.getLastEntered());
PendingIntent pendingIntent = PendingIntent.getBroadcast(这一点,ua.getId()
intenta,PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),pendingIntent);
}
}
与NotificationMenu被通知,这就是为什么我使用AlarmManager
解决方案我不知道如果我理解引导接收器,以及如何重新启动所有报警。
刚刚打电话给你的code调用 setRepeating()
(或其他)的 AlarmManager
。
例如,在此示例项目, PollReceiver
设置为接收 BOOT_COMPLETED
。在的onReceive()
,它重新安排报警:
包com.commonsware.android.schedsvc;
进口android.app.AlarmManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.SystemClock;
公共类PollReceiver扩展的BroadcastReceiver {
私有静态最终诠释周期= 5000;
@覆盖
公共无效的onReceive(上下文ctxt,意图我){
scheduleAlarms(ctxt);
}
静态无效scheduleAlarms(上下文ctxt){
AlarmManager经理=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
意图I =新的意图(ctxt,ScheduledService.class);
PendingIntent圆周率= PendingIntent.getService(ctxt,0,I,0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime()+周期,周期,PI);
}
}
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?
Cheers
Code :
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);
}
}
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.
Just call your code to call setRepeating()
(or whatever) on AlarmManager
.
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);
}
}