Android WorkManager PeriodicWorkRequest issue — not running after app is closed

huangapple go评论93阅读模式
英文:

Android WorkManager PeriodicWorkRequest issue -- not running after app is closed

问题

我一直在尝试从Firebase Job Dispatcher迁移到androidx Work Manager。我的应用程序在使用Job Dispatcher时运行良好。Work Manager的问题在于,在应用关闭后它不执行周期性工作。没有周期性工作运行,用户无法接收通知。

我的应用程序正在多个Pixel模拟器上进行测试(例如,SDK级别30),从Android Studio启动。我期望这些模拟器的行为与标准的Android行为相同。我还使用了物理手机进行测试,并遇到了相同的问题。

我一直在寻找解决方案,但目前还没有找到。您能帮助识别问题的原因和解决方案吗?

以下是我的Java代码:

Constraints constraints = new Constraints.Builder()
    .setRequiredNetworkType(NetworkType.CONNECTED)
    .build();

PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(MyClass.class, 15, TimeUnit.MINUTES)
    .addTag("my_tag")
    .setConstraints(constraints)
    .setInitialDelay(15, TimeUnit.MINUTES)
    .build();

WorkManager.getInstance(appContext)
    .enqueueUniquePeriodicWork("my_unique_work_name", ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest);

Log.d("From Work scheduling procedure...", "periodicWorkRequest enqueued");

以下是我的build.gradle(app级别)中的配置:

implementation 'androidx.work:work-runtime:2.4.0'
英文:

I have been trying to migrate from Firebase Job Dispatcher to androidx Work Manager. My app was working well with Job Dispatcher. The problem with Work Manager is that it doesn't execute the Periodic Work after the app is closed. Without Periodic Work running, users cannot receive notifications.

My app is being tested on several Pixel Emulators (e.g. SDK Level 30) launching from Android Studio. I expected stock Android behavior from those emulators. I also tested by using physical phones and got the same problem.

I have been searching for the solution but so far without luck. Could you help identifying the cause and the solution?

Here is my Java code:

    Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();

    PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(MyClass.class, 15, TimeUnit.MINUTES)
            .addTag("my_tag")
            .setConstraints(constraints)
            .setInitialDelay(15, TimeUnit.MINUTES)
            .build();

    WorkManager.getInstance(appContext)
            .enqueueUniquePeriodicWork("my_unique_work_name", ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest);

    Log.d ( "From Work scheduling procedure...", "periodicWorkRequest enqueued" );

Here is my configuration in build.gradle (app level)

    implementation 'androidx.work:work-runtime:2.4.0'

答案1

得分: 1

经过一天多的搜索,我通过多次尝试找到了答案。问题并不是关于我的程序,而是关于Logcat的特定行为。

在关闭模拟器上的我的应用后,我注意到Logcat的进程下拉菜单显示为“没有可调试的进程”。这就是为什么我无法看到我的调试消息或相关的周期性工作消息的原因。当我重新启动应用程序时,我的应用程序会再次出现在下拉菜单中作为可调试的进程,之前执行的所有周期性工作项目都会在Logcat中显示出来。

对于我测试过的手机,当我在连接USB电缆的情况下关闭我的应用程序后,我注意到Logcat的进程下拉菜单仍然将我的应用程序保持为可调试的进程。我变得更加耐心等待和观察。我的周期性工作的调试消息出现了。

我的周期性工作实际上是在我的模拟器和手机上执行的。我没有在Logcat中看到日志,所以我以为我的程序运行不好。我的程序其实是正常工作的,只是我没有足够了解Logcat。

在寻找答案时,我遇到了许多类似的问题。希望我的答案能帮助一些人节省一些时间。

英文:

After searching for the answer for over a day, I got it by having many rounds of trial and error. The issue is not about my program but a specific behavior of Logcat.

After I closed my app on an emulator, I noticed that the Logcat's process drop-down menu showed "No debuggable processes". That was the reason I couldn't see my debugging messages or the related Periodic Work messages logged. When I re-launched the app, my app re-appeared as the debuggable process in the drop-down menu, and all the Periodic Work items executed earlier appeared in Logcat.

For a phone that I tested, after I closed my app on it while having the USB cable connected, I noticed that the Logcat's process drop-down menu remained having my app as the debuggable process. I became more patient waiting and observing. My debugging messages for Periodic Work appeared.

My Periodic Work items were actually being executed on my emulators and phones. I didn't see the logs in Logcat and I assumed my program was not working well. My program was working well but I didn't understand Logcat enough.

I came across many similar questions when I was searching for the answer. Hopefully my answer helps saving some of you some time.

Android WorkManager PeriodicWorkRequest issue — not running after app is closed

huangapple
  • 本文由 发表于 2020年8月10日 03:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63330582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定