【问题标题】:How to set the exact time to android job for a daily job at fixed time?如何在固定时间为每天的工作设置准确的时间到android工作?
【发布时间】:2018-05-22 16:56:34
【问题描述】:

我正在尝试安排每天晚上 10 点运行的作业。

我尝试使用 setExact 方法并通过将 22 小时转换为毫秒来提供毫秒数,为了进行测试,我执行了应用程序并将系统时间更改为晚上 10 点,但作业没有执行。

我还尝试给出即将到来的时间,因此将 12:45 转换为毫秒并提供给 setExact 方法。但这也没有用。

如何设置和测试?

文件跟踪作业

class FileTrackJob extends Job {

        static final String TAG = "FileTracking";

        @NonNull
        @Override
        protected Result onRunJob(Params params) {

            PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0,
                    new Intent(getContext(), MainActivity.class), 0);

            Calendar cal = Calendar.getInstance();
            Date currentLocalTime = cal.getTime();
            DateFormat date = new SimpleDateFormat("HH:mm a");
    // you can get seconds by adding  "...:ss" to it
            String localTime = date.format(currentLocalTime);

            Notification notification = new NotificationCompat.Builder(getContext())
                    .setContentTitle("Android Job Demo")
                    .setContentText("Notification from Android Job Demo App. " + localTime)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setShowWhen(true)
                    .setColor(Color.RED)
                    .setLocalOnly(true)
                    .build();

            NotificationManagerCompat.from(getContext())
                    .notify(new Random().nextInt(), notification);


            return Result.SUCCESS;
        }

        static void scheduleNoti() {
            new JobRequest.Builder(TrackingJob.TAG)
                    //  .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(15))
                    .setExact(44820000)
                    .setUpdateCurrent(true)
                    .setPersisted(true)
                    .build()
                    .schedule();
        }
    }

主活动

FileTrackJob.scheduleNoti();

主应用程序

public class MainApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        JobManager.create(this).addJobCreator(new DemoJobCreator());
    }
}

DemoJobCreator

class DemoJobCreator implements JobCreator {

    @Override
    public Job create(String tag) {
        switch (tag) {
            case TrackingJob.TAG:
                return new TrackingJob();

            case FileTrackJob.TAG:
                return new FileTrackJob();
            default:
                return null;
        }
    }
}

我还安排了一项定期工作,这项工作不适用于 red mi 等某些设备,在一台三星设备上没有重复,但在 moto g4 plus 上运行良好。

class TrackingJob extends Job {

    static final String TAG = "tracking";

    @NonNull
    @Override
    protected Result onRunJob(Params params) {

        Intent pi = new Intent(getContext(), GetLocationService.class);
        getContext().startService(pi);


        return Result.SUCCESS;
    }

    static void schedulePeriodic() {
        new JobRequest.Builder(TrackingJob.TAG)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(15))
                .setUpdateCurrent(true)
                .setPersisted(true)
                .build()
                .schedule();
    }
}

有人可以帮忙吗?谢谢。。

【问题讨论】:

  • 你试过报警管理器了吗?
  • 是的,我尝试使用警报管理器。但它有局限性,如果手机关闭它不会触发警报,它也不会与 red mi 一起使用,如果应用程序在 ram 中则只会触发警报。它不允许 red mi OS 中的自动权限。所以我想切换到其他调度程序。 @快速学习者
  • 好吧,当手机可能重启时,你必须重启它
  • 但不适用于 red mi 设备。这个Android工作也不适用于red mi。使用哪个调度器?此外,我观察到其他设备也有几次错过警报。 @快速学习者

标签: java android android-jobscheduler android-job


【解决方案1】:

您必须使用DailyJob 而不是Job 才能在每天的特定时间做某事。

public static final class MyDailyJob extends DailyJob {

    public static final String TAG = "MyDailyJob";

    public static void schedule() {
        // schedule between 1 and 6 *PM*
        DailyJob.schedule(new JobRequest.Builder(TAG), TimeUnit.HOURS.toMillis(13), TimeUnit.HOURS.toMillis(18));
    }

    @NonNull
    @Override
    protected DailyJobResult onRunDailyJob(Params params) {
        return DailyJobResult.SUCCESS;
    }
}

另外:如果要设置特定的分钟和秒,请使用(假设在 15:48:05 和 17:42:09 之间):

    DailyJob.schedule(new JobRequest.Builder(TAG), TimeUnit.HOURS.toMillis(15)+TimeUnit.MINUTES.toMillis(48)+TimeUnit.SECONDS.toMillis(5), 
TimeUnit.HOURS.toMillis(17)+TimeUnit.MINUTES.toMillis(42)+TimeUnit.SECONDS.toMillis(9));

希望这会有所帮助。

【讨论】:

  • 是否可以在准确的时间安排例如。 23:59:59 ?
  • 但是即使应用程序未打开,它也会运行吗?另外,用户必须打开应用程序才能安排工作,对吗?即使用户没有打开应用程序,有没有办法做到这一点?假设用户安装了应用程序并且从不打开它,但我想每天向用户发送通知。
  • @AnirudhGanesh 这个答案很老了,当时它曾经很好用。另外,这个库现在已经过时了。我们现在有 work-manager,它使用 gurrantee* 执行作业。 developer.android.com/topic/libraries/architecture/workmanager/…
  • 它至少没有被弃用!,我可以使用它。我还使用工作管理器来构建定期请求和一次性请求,但同样,为了在准确的时间运行工作,谷歌仍然推荐警报管理器和作业 API。 Jobs Api 的有趣之处在于,当设备进入打盹模式时,我认为它不会运行!我在不同的设备上进行了尝试,对于旧的 API,它运行顺利,但在新的 API 中必须求助于其他东西。
  • 是的,谷歌现在非常关心电池。甚至一些 3rd 方 OEM 现在也打破了正常功能以压缩最少的电池量。 dontkillmyapp.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 2012-05-09
  • 2017-05-28
  • 2011-09-04
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多