启动时间都会更新每次应用程序启动

发布于 2025-02-08 16:10:56 字数 1853 浏览 2 评论 0原文

需求 - 应用都需要在特定时间每天登录,而不论应用程序在后台还是前面

Workmanager 具有定期的最初延迟工作,可以在特定时间设置并定期运行。

easudy - schedule_requested_at_at 都会更新应用程序可在 WorksPec 表中启动。尽管 initial_delay 保持不变。使用的应用程序检查查看数据库。

应用程序检查>背景任务检查员>工人 >注销工作者>每次启动应用程序都会开始更新。

尝试过,

  • 我们添加了偏好的标志,以检查我们没有再次更新工人。尽管我们不再设置它。从 initial_delay 中可以知道,在表中是恒定的。

以下是我们用于Workmanager的设置。

  • 我们使用使用enqueueuniqueeperiodicwork
val refreshWork = PeriodicWorkRequestBuilder<LogOutWorker>(
            interval.toLong(), TimeUnit.HOURS
        ).setInitialDelay(minutes, TimeUnit.MINUTES)
            .setConstraints(constraints)
            .addTag(tag)
            .build()

        WorkManager.getInstance(context)
            .enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP, refreshWork)
  • 从清单中删除Workmanager初始化。
<provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
</provider>
  • 我们已经在应用程序中添加了只有基本日志启用的初始化。
@HiltAndroidApp
class App : Application(), Configuration.Provider {

@Inject
lateinit var logoutWorkerFactory: LogOutWorkerFactory

override fun onCreate() {
        super.onCreate()
  WorkManager.initialize(this, workManagerConfiguration)
}

override fun getWorkManagerConfiguration(): Configuration {
        return Configuration.Builder()
            .setWorkerFactory(logoutWorkerFactory)
            .setMinimumLoggingLevel(Log.DEBUG)
            .build()
    }
}

Requirement - App needs to be logged out everyday at specific time irrespective of whether app is in background or foregound.

Solution - Run a snippet every day to logout user from the app. WorkManager has periodic work with initial delay which can be set at specific time and runs periodically.

Issue - schedule_requested_at is updated every time app is launched available under WorkSpec table. Though initial_delay remains the same. Used App Inspection to view Database.

Same can be visible under App Inspection > Background Task Inspector > Worker > LogOutWorker > Start updated every-time app is launched.

Tried

  • We have added a flag in preferences to check that we are not updating worker again. Though we are not setting it again. Which can be known from initial_delay is constant in the table.

Following is the set-up we are using for workManager.

  • We are using PeriodicWorkRequestBuilder with enqueueUniquePeriodicWork
val refreshWork = PeriodicWorkRequestBuilder<LogOutWorker>(
            interval.toLong(), TimeUnit.HOURS
        ).setInitialDelay(minutes, TimeUnit.MINUTES)
            .setConstraints(constraints)
            .addTag(tag)
            .build()

        WorkManager.getInstance(context)
            .enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP, refreshWork)
  • Removing workManager initialization from manifest.
<provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
</provider>
  • We have added initialisation in the app with basic log enables only.
@HiltAndroidApp
class App : Application(), Configuration.Provider {

@Inject
lateinit var logoutWorkerFactory: LogOutWorkerFactory

override fun onCreate() {
        super.onCreate()
  WorkManager.initialize(this, workManagerConfiguration)
}

override fun getWorkManagerConfiguration(): Configuration {
        return Configuration.Builder()
            .setWorkerFactory(logoutWorkerFactory)
            .setMinimumLoggingLevel(Log.DEBUG)
            .build()
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文