即使没有启动该应用程序,Android的Workmanager的工人即使该应用程序也开始运行?
我正在遵循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。如果我们完全关闭了应用程序 - 工人仍然可以工作吗?,
如果您有专门讨论这些问题的消息来源,我很想阅读它们!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题
必须至少启动一次应用程序来安排工作,安装应用程序还不够。
第二个问题
手机需要完成靴子并已解锁一次。这是因为工人被标记为不直接启动。您可以在本指南上,您可以在
手机完成启动后,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.
第一个问题 - 该应用程序必须至少启动一次才能安排工作。
第二个问题没有明确的答案。
根据 docs
和
但是可能有一些例外,请参见例如。
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
and
But there may be some exceptions, see this answer for example.