即使没有启动该应用程序,Android的Workmanager的工人即使该应用程序也开始运行?

发布于 2025-01-22 14:34:44 字数 1553 浏览 0 评论 0 原文

我正在遵循Udacity Android Kotlin开发人员课程。在其中的一门课程中,讲师会教授使用Workmanager执行背景任务,以便在后台缓存数据,以在应用程序开始时显示新的数据。

因此,在应用程序的主要应用程序中定义了定期定义用于启动Workmanager刷新数据的代码。

class DevByteApplication : Application() {

/**
 * onCreate is called before the first screen is shown to the user.
 *
 * Use it to setup any background tasks, running expensive setup operations in a background
 * thread to avoid delaying app start.
 */
val applicationScope = CoroutineScope(Dispatchers.Default)

override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())
    delayedInit()
}

private fun delayedInit() = applicationScope.launch {
    setupRecurringWork()
}

private fun setupRecurringWork() {
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.UNMETERED)
        .setRequiresBatteryNotLow(true)
        .setRequiresCharging(true)
        .apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                setRequiresDeviceIdle(true)
            }
        }.build()

    val repeatingRequest = PeriodicWorkRequestBuilder<RefreshDataWorker>(1, TimeUnit.DAYS)
        .build()

    WorkManager.getInstance().enqueueUniquePeriodicWork(
        RefreshDataWorker.WORK_NAME,
        ExistingPeriodicWorkPolicy.KEEP,
        repeatingRequest)
}}

问题是:

那么,在启动一次应用程序时,工人管理器才能开始工作?还是安装应用程序后它会开始工作?

1。如果我们完全关闭手机 - 一旦我们打开电话,我们的应用程序工作经理就会工作 2。如果我们完全关闭了应用程序 - 工人仍然可以工作吗?

如果您有专门讨论这些问题的消息来源,我很想阅读它们!

I am following the Udacity Android Kotlin Developer course. In one of the lessons, the instructor taught about doing background tasks using WorkManager always to cache data in the background to show fresh data at the app's start.

So the code to start WorkManager refreshing data periodically is defined in the Main Application of the app.

class DevByteApplication : Application() {

/**
 * onCreate is called before the first screen is shown to the user.
 *
 * Use it to setup any background tasks, running expensive setup operations in a background
 * thread to avoid delaying app start.
 */
val applicationScope = CoroutineScope(Dispatchers.Default)

override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())
    delayedInit()
}

private fun delayedInit() = applicationScope.launch {
    setupRecurringWork()
}

private fun setupRecurringWork() {
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.UNMETERED)
        .setRequiresBatteryNotLow(true)
        .setRequiresCharging(true)
        .apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                setRequiresDeviceIdle(true)
            }
        }.build()

    val repeatingRequest = PeriodicWorkRequestBuilder<RefreshDataWorker>(1, TimeUnit.DAYS)
        .build()

    WorkManager.getInstance().enqueueUniquePeriodicWork(
        RefreshDataWorker.WORK_NAME,
        ExistingPeriodicWorkPolicy.KEEP,
        repeatingRequest)
}}

Questions are:

So does WorkManager only start to work if an app is launched once? Or does it start to work once we install the application?

Also,

1. If we shut down the phone completely - will the WorkManager of our app work once we turn on the phone back
2. If we close the application completely - will the WorkManager still work?

If you have sources that talk specifically about these questions, I would love to read them!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-01-29 14:34:44

第一个问题

必须至少启动一次应用程序来安排工作,安装应用程序还不够。

第二个问题

手机需要完成靴子并已解锁一次。这是因为工人被标记为不直接启动。您可以在本指南上,您可以在

手机完成启动后,Workmanager将独立执行预定的工作,而不是启动或不启动应用程序。

唯一的例外是用于力停止。在这种情况下,所有通知和所有计划的作业(由于工人依赖于Android的)被取消,直到用户启动应用程序为止。

一旦执行了一次应用程序,Workmanager就会拿起所有工作并重新安排它。

文档

workmanager文档非常好,并且涵盖了所有API表面可在 android开发者频道youtube 上。

First question

The application must be launched at least once to schedule work, installing the application is not enough.

Second question

The phone needs to have completed the boot and been unlocked once. This because WorkManager is marked as not direct boot aware. You can read more about Direct Boot Mode on this guide.

Once the phone completed its boot, WorkManager will execute the scheduled work independently from having launched or not the application.

The only exception is for application that have been force stopped. In this case all notification and all scheduled Jobs (as WorkManager relies to Android's JobScheduler) are cancelled till the application is launched by the user.

Once the application is executed at least once, WorkManager will pick up all the Work and reschedule it.

Documentation

WorkManager documentation is quite good and covers all the API surface, some additional content is available on this blog series.
Videos about WorkManager are available on the Android Developer channel on youtube.

相思碎 2025-01-29 14:34:44

第一个问题 - 该应用程序必须至少启动一次才能安排工作。

第二个问题没有明确的答案。

根据 docs

计划的工作存储在内部管理的SQLITE数据库中,并且
工人照顾确保这项工作持续存在,并且是
重新安排在设备重新启动之间。

工人旨在用于可靠运行的工作
如果用户从屏幕上导航,应用程序退出或设备
重新启动。

但是可能有一些例外,请参见例如。

First question - the application must be launched at least once to schedule work.

There is no clear answer to the second question.

According to docs

Scheduled work is stored in an internally managed SQLite database and
WorkManager takes care of ensuring that this work persists and is
rescheduled across device reboots.

and

WorkManager is intended for work that is required to run reliably even
if the user navigates off a screen, the app exits, or the device
restarts.

But there may be some exceptions, see this answer for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文