我想通过一个例子来表明我的问题:
我设置RTC_wakeup重复到上午10点触发IntentService,当我在上午9:30运行我的应用程序时,它等到上午10点并触发服务(太棒了!)。
但是当我在10:30运行我的应用程序时,它确定计划的时间已经过去,因此它会触发服务(不是我想要的)。
我只想让我的服务每天都被触发一次,恰好在上午10点,我该怎么办? (问题是应用程序在上午10点之后运行它会触发服务)
这是我的代码:
public class AlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ServiceFTP_LogFileUpload.class);
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, service);
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
/* Set the alarm to start at 10:00 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
public void cancelAlarm(Context context) {
// If the alarm has been set, cancel it.
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}
}
}
警报在SplashScreen(启动器活动)中设置:
AlarmReceiver alarm = new AlarmReceiver();
alarm.setAlarm(this);