如何每小时执行一项任务?

时间:2021-01-02 08:01:36

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

我一直在开发Android应用程序,我需要每小时执行1个任务。我使用以下代码:

private static final long ALARM_PERIOD = 1000L;

public static void initAlarmManager(Context context) {

    Editor editor=PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean(context.getString(R.string.terminate_key), true).commit();

    AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmEventReceiver.class);
    PendingIntent receiver = PendingIntent.getBroadcast(context, 0, i, 0);
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(), ALARM_PERIOD, receiver);
}

It works for me, but my client tells me that the task works only 1 time and won't work 1 hour. Where have I made a mistake? Please, tell me. Thank you.

它适用于我,但我的客户告诉我,该任务只工作一次,不会工作1小时。我哪里弄错了?请告诉我。谢谢。

4 个解决方案

#1


3  

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

根据您的代码,ALARM_PERIOD是1000L,作为重复间隔。所以我怀疑每1000毫秒会发出一次警报。

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.

如果您为每小时设置重复间隔,则应为3600000L。请注意,如果重新启动手机,除非您重新开始,否则您的闹钟管理器将不再有效。

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
}

#2


2  

Have you added receiver tag in application tag in manifest.xml
<receiver android:name=".AlarmReceiver" android:process=":remote"/>

你有没有在manifest.xml中的应用程序标签中添加接收器标签

#3


1  

Instead of Alram-Manager I recommended you to use Android-TimerTask

我推荐你使用Android-TimerTask而不是Alram-Manager

The TimerTask class represents a task to run at a specified time. The task may be run once or repeatedly. Its perfect suits for your requirements.

TimerTask类表示在指定时间运行的任务。任务可以运行一次或重复运行。它完美适合您的要求。

#4


0  

Try by modifying your code by changing your setRepeating() method like this

尝试通过更改这样的setRepeating()方法来修改代码

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(), SystemClock.elapsedRealtime()+(60*60*1000), receiver);

OR

要么

Test this it is repeating for every minute

测试它每分钟重复一次

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,Calendar.getInstance().getTimeInMillis(), Calendar.getInstance().getTimeInMillis()+(1*60*1000), receiver);

#1


3  

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

根据您的代码,ALARM_PERIOD是1000L,作为重复间隔。所以我怀疑每1000毫秒会发出一次警报。

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.

如果您为每小时设置重复间隔,则应为3600000L。请注意,如果重新启动手机,除非您重新开始,否则您的闹钟管理器将不再有效。

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
}

#2


2  

Have you added receiver tag in application tag in manifest.xml
<receiver android:name=".AlarmReceiver" android:process=":remote"/>

你有没有在manifest.xml中的应用程序标签中添加接收器标签

#3


1  

Instead of Alram-Manager I recommended you to use Android-TimerTask

我推荐你使用Android-TimerTask而不是Alram-Manager

The TimerTask class represents a task to run at a specified time. The task may be run once or repeatedly. Its perfect suits for your requirements.

TimerTask类表示在指定时间运行的任务。任务可以运行一次或重复运行。它完美适合您的要求。

#4


0  

Try by modifying your code by changing your setRepeating() method like this

尝试通过更改这样的setRepeating()方法来修改代码

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(), SystemClock.elapsedRealtime()+(60*60*1000), receiver);

OR

要么

Test this it is repeating for every minute

测试它每分钟重复一次

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,Calendar.getInstance().getTimeInMillis(), Calendar.getInstance().getTimeInMillis()+(1*60*1000), receiver);