WorkManager的15分钟的PeriodicWorkRequest不能连续运行6个多小时 [英] WorkManager's 15 minute PeriodicWorkRequest doesn't run for 6+ hours straight

查看:126
本文介绍了WorkManager的15分钟的PeriodicWorkRequest不能连续运行6个多小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题:

在图像中可见,红色框表示WorkManager没有运行请求的时间间隔.我可以确保该应用程序自安装以来从未被强行杀死.一段时间以来,它已被清除,但似乎没有立即产生作用.

这是我初始化请求的方式:

  PeriodicWorkRequest checkLastSeenRequest =新的PeriodicWorkRequest.Builder(LastSeenWorker.class,PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,Tim​​eUnit.MILLISECONDS,PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS,Tim​​eUnit.MILLISECONDS).setBackoffCriteria(BackoffPolicy.LINEAR,PeriodicWorkRequest.MIN_BACKOFF_MILLIS,TimeUnit.MILLISECONDS).建造();WorkManager.getInstance(this).enqueueUniquePeriodicWork("checkLastSeen",现有的PeriodicWorkPolicy.KEEP,checkLastSeenRequest); 

这是 RxWorker createWork()方法:

  @NonNull@Override公共Single< Result>createWork(){return Single.create(subscriber-> {FirebaseFirestore firestore = FirebaseFirestore.getInstance();firestore.collection(用户").document("doc").get().addOnCompleteListener(task-> {如果(task.isSuccessful()){DocumentSnapshot doc = task.getResult();如果(doc!= null&& doc.exists()&& doc.getData()!= null&&doc.getData().get("lastSeen")!= null){->//这是我登录的地方Subscriber.onSuccess(Result.success());} 别的 {->XLog.e(找不到要读取的文档");subscription.onError(new Error(找不到文档")));}} 别的 {->XLog.e(task.getException());subscription.onError(task.getException());}});});} 

此处的文档中,我的理解是在打ze模式下,还有一个窗口,即使所有请求变得不那么频繁,也可以执行所有请求.那么为什么完全停电超过6个小时??是否需要获得电池优化许可才能使电池正常工作?任何帮助表示赞赏!

解决方案

  1. 尝试按照描述的方式在打ze"模式下检查您的代码一些有用的信息

    1. 也许您还有另一种选择:使用WorkManager计划任务

    Similar question: Android workmanager scheduled worker lost after task killed
    The answer here suggests that this is a problem with specific manufacturers, but I am using an Android One phone which has the unmodified OS.

    For the most part my PeriodicWorkRequest works as expected, but then for a straight 5-8 hours window, it doesn't get executed at all. I log whenever the request is executed in a log file, and this is how it looks:

    As visible in the image, the red box indicates the interval where WorkManager did not run the request. I can assure that the app was never force killed since install. It was cleared from recents some times, but that did not seem to have any immediate effects.

    This is how I initialized the request:

    PeriodicWorkRequest checkLastSeenRequest =
                    new PeriodicWorkRequest.Builder(LastSeenWorker.class,
                            PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
                            PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS)
                            .setBackoffCriteria(
                                    BackoffPolicy.LINEAR,
                                    PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
                                    TimeUnit.MILLISECONDS)
                            .build();
            WorkManager.getInstance(this).enqueueUniquePeriodicWork(
                    "checkLastSeen",
                    ExistingPeriodicWorkPolicy.KEEP,
                    checkLastSeenRequest);
    

    And this is the createWork() method of RxWorker:

    @NonNull
    @Override
    public Single<Result> createWork() {
    
        return Single.create(subscriber -> {
            FirebaseFirestore firestore = FirebaseFirestore.getInstance();
            firestore.collection("users").document("doc").get()
                    .addOnCompleteListener(task -> {
                        if (task.isSuccessful()) {
    
                            DocumentSnapshot doc = task.getResult();
    
                            if (doc != null && doc.exists() && doc.getData() != null
                                        && doc.getData().get("lastSeen") != null) {
    
    -->                         // this is where I log
    
                                subscriber.onSuccess(Result.success());
                            } else {
    -->                         XLog.e("No document found to read from");
                                subscriber.onError(new Error("Document not found"));
                            }
                        } else {
    -->                     XLog.e(task.getException());
                            subscriber.onError(task.getException());
                        }
                    });
        });
    }
    

    From the documentation here my understanding is that in doze mode as well there is a window where all the requests are allowed to execute even as these become less frequent. So why is there a complete blackout for more than 6 hours? Do I need to get battery optimization permissions for it to work as expected? Any help appreciated!

    解决方案

    1. try to check working your code in the Doze mode as described it here

    Maintenance windows in Doze won’t happen frequently. First window occurs an hour after last activity, the next after two, the next after four and so on. But it doesn’t matter that much since user isn’t even moving her device anyway.

    Some useful information

    1. Maybe you have another choice: Schedule tasks with WorkManager

    这篇关于WorkManager的15分钟的PeriodicWorkRequest不能连续运行6个多小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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