但是,在我的代码的最后一次更改之后,我的"定时器接收器"有效,但不是......
我可以启动管理器并调用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