我想每天只在特定时间运行服务运行, 让说,每天8:00 ,在这里我找到了两种方法服务于特定时间的Android
public static void setScheduleMessage(Context context, int hours,
int minuts, int seconds) {
Calendar calendar = Calendar.getInstance();
// 8 AM
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minuts);
calendar.set(Calendar.SECOND, seconds);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
new Intent(context, MessageSchduledService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
但问题此方法是指定时间后每小时运行一次。
这里是第二种方法,我用它不执行我的服务?
public static void setScheduleMessage2(Context context, int hours,
int minuts, int seconds) {
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current
// time and date for
// this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, hours);
cal.set(Calendar.MINUTE, minuts);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(context, MessageSchduledService.class);
PendingIntent pintent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pintent);
}
POST: 在我的应用我只是想从我的应用程序数据库进行信息数据的基础上,在特定的时间,每天推送消息。 如果有人对此有所了解,那么让我知道我在这些方法中做错了什么?
2013-11-23
khurram