我怎么能每小时使用一次?现在每天都在工作16.30

时间:2017-07-25 11:14:01

标签: android calendar

我正在创建通知系统我需要每小时发出通知,但现在它只提供一天通知

protected void onStart() {
        super.onStart();

            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.HOUR_OF_DAY, 16);
            calendar.set(Calendar.MINUTE, 30);

            Intent intent = new Intent(getApplicationContext(),NotificationReceiver.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY,pendingIntent);

        }

3 个答案:

答案 0 :(得分:3)

您正在使用alarmManager.INTERVAL_DAY因此会每24小时执行一次,并使用alarmManager.INTERVAL_HOUR更改

答案 1 :(得分:1)

     alarmManager. setRepeating  (AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),  alarmManager.INTERVAL_HOUR,   pendingIntent);

答案 2 :(得分:0)

作为替代方案,您可以编写自己的类来扩展TimerTask,如

public class ReminderTask extends TimerTask {
    public void run() {
       System.out.println("Reminding you of something!"); 
    }
}

然后像这样开始:

Timer timer = new Timer();
timer.schedule(new ReminderTask(), 0, 3600000); // one hour in millis

但不可否认,Oussema Aroua的回答是在这种情况下的方法......