安排任务每24小时运行一次 [英] Scheduling a task to run every 24 hours

查看:85
本文介绍了安排任务每24小时运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Android应用程序,该应用程序每24小时会增加一个计数器.我只是在使用SharedPreferences,当它是时候,它会增加共享首选项中的值.现在,我使用了FirebaseJobDispatcher,但是这样做的问题是,它的增量不一致.我通读了一些文档,我的理解是,当涉及一些网络调用时,最好使用FirebaseJobDispatcher.我的问题是,我如何安排一个简单的作业在android中每24小时运行一次?请任何帮助和建议将非常有帮助.以下是我的FirebaseJobDispatcher代码.现在我只用2分钟来检查.

I am trying to create an Android app that increments a counter after every 24 hours. I am just making use of SharedPreferences and when it is time it increments the value in shared preferences. Right now I made use of FirebaseJobDispatcher but the problem with this was that it is inconsistent in incrementing. I read through some documents and my understanding is that it is better to use FirebaseJobDispatcher when it involves some network calls as well. My question is that how can I schedule a simple job in android to run every 24 hours? Please any help and suggestions would be very helpful. Below is my FirebaseJobDispatcher code. Right now I am just using using 2 minutes just for checking purposes.

public class ScheduleIncrementJob {

private static final int UPDATE_INTERVAL_MINUTES = 2;
private static final int UPDATE_INTERVAL_SECONDS = (int)(TimeUnit.MINUTES.toSeconds(UPDATE_INTERVAL_MINUTES));
private static final int SYNC_FLEX_SECONDS = UPDATE_INTERVAL_SECONDS;

private static final String UPDATE_JOB_TAG = "update_counter_tag";

private static boolean sInitialized;

synchronized public static void scheduleUpdateCounter(final Context context){
    if(sInitialized)return;

    Driver driver = new GooglePlayDriver(context);
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);

    Job constraintUpdateJob = dispatcher.newJobBuilder()
            .setService(IncrementJobService.class)
            .setTag(UPDATE_JOB_TAG)
            .setLifetime(Lifetime.FOREVER)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(UPDATE_INTERVAL_SECONDS,
                    UPDATE_INTERVAL_SECONDS + SYNC_FLEX_SECONDS))
            .setReplaceCurrent(true)
            .build();
    dispatcher.schedule(constraintUpdateJob);
    sInitialized = true;
}
}

推荐答案

您可以使用AlarmManager类使用 PendingIntent 计划重复的警报,该警报将在计划时完成. https://developer.android.com/training/scheduling/alarms

You can use AlarmManager class to schedule a repeating alarm with PendingIntent that will do your job when scheduled. https://developer.android.com/training/scheduling/alarms

我将为您提供被解雇后的DailyAlarm示例:

I'll give you sample of dailyAlarm doing some job when fired:

 public void setDailyAlarmOn(Context context, long alarmTime, Uri reminderTask, long repeatTime) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


        PendingIntent operation =
                yourJobHere.getReminderPendingIntent(context, reminderTask);

        if (Build.VERSION.SDK_INT >= 23) {

            manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, repeatTime, operation);
        } else {
            manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,  repeatTime, operation);
        }

    }

alarmTime-应该以分钟为单位的第一次触发警报
repeatTime-以毫秒为单位的时间-86400000 for 24h(1000 * 60 * 60 * 24)
Uri提醒任务-我创建的uri是为了不取消先前的警报,简而言之,它是警报应用程序的代码,它是数据库中行中的uri.
操作-您需要创建的PendingIntent

alarmTime - first time in miliseconds when alarm is supposed to be fired
repeatTime - time in mili seconds - 86400000 for 24h (1000*60*60*24)
Uri reminderTask - uri that I created to not to cancel previous alarms, in short, it's a code for alarm application and it's uri from row in database.
operation - PendingIntent which you'll need to create

我通过扩展IntentService并在 onHandleIntent 中处理作业来做到这一点:YourJobHere.class:

I did it by extending IntentService and handling job in onHandleIntent: YourJobHere.class:

public class YourJobHere extends IntentService {
    private static final String TAG = YourJobHere.class.getSimpleName();

    public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
        Intent action = new Intent(context, YourJobHere.class);
        action.setData(uri);
        return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public YourJobHere() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        //TODO: make your job here

      }
}

如果需要,可以在GitLab上参考我的完整项目: https://gitlab.com/Domin_PL/SlothProfileScheduler

If you want you can refer to my full project on GitLab: https://gitlab.com/Domin_PL/SlothProfileScheduler

希望它会为您提供帮助

这篇关于安排任务每24小时运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆