我虽然在我的代码的最后一次更改之后,我的“定时器接收器”工作,但它不是......
我可以启动管理器并调用 Receiver,然后,几个小时后,AlarmManager 停止工作。
我的应用程序中有一个代码来检查 AlarmManager 是否运行,它说它确实运行,但不会调用 Receiver。
这是我的代码:
private AlarmManager alarmMgr;
private PendingIntent checkPendingIntent;
public void initTimer()
{
this.alarmMgr = (AlarmManager)this.main.getSystemService(Context.ALARM_SERVICE);
this.alarmActive = (PendingIntent.getBroadcast(this.main, 0,
new Intent(this.main, AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) != null);
this.checkPendingIntent = PendingIntent.getBroadcast(this.main, 0,
new Intent(this.main, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
}
public void startTimer(String frequency)
{
Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
long millis = 0;
if(frequency.compareTo("1HOUR") == 0)
millis = AlarmManager.INTERVAL_HOUR;
if(frequency.compareTo("12HOUR") == 0)
millis = AlarmManager.INTERVAL_HALF_DAY;
if(frequency.compareTo("1DAY") == 0)
millis = AlarmManager.INTERVAL_DAY;
if(frequency.compareTo("1WEEK") == 0)
millis = 7 * AlarmManager.INTERVAL_DAY;
calendar.setTimeInMillis(System.currentTimeMillis());
if(calendar.before(now))
calendar.add(Calendar.DATE, 1);
if(!this.timerSet)
Logger.debug("Starting update checker (" + frequency + ")");
else
Logger.debug("Restarting update checker (" + frequency + ")");
this.timerSet = true;
this.alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
millis, this.checkPendingIntent);
}
好吧,this.alarmActive 是真的,所以我希望它可以工作,当然不会再次调用 startTimer 方法。但是我可以在我的手机和服务器上的日志中看到(接收器只是发送一个 HTTP 请求),接收器不会被调用......
有人可以说我,我做错了什么吗?
非常感谢
Luca Bertoncello