How to execute one task every hour?

后端 未结 4 1751
天命终不由人
天命终不由人 2020-12-15 01:58

I have been developing an Android application and I need to execute 1 task every hour. I uses the following code for it:

private static final long ALARM_PERI         


        
4条回答
  •  执笔经年
    2020-12-15 02:47

    According to your code, ALARM_PERIOD is 1000L, as repeating interval. So I doubt the alarm will set of in every 1000 milliseconds.

    if you are setting repeating interval for every hour, it should be 3600000L. And take note that if the phone is restarted, your alarm manager will no longer work unless you start again.

    Here is the my Code:

    private void setAlarmManager() {
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        long l = new Date().getTime();
        if (l < new Date().getTime()) {
            l += 86400000; // start at next 24 hour
        }
        am.setRepeating(AlarmManager.RTC_WAKEUP, l, 86400000, sender); // 86400000
    }
    

提交回复
热议问题