android studio workmanager 每天在特定时间一次

发布于 2025-01-16 02:11:17 字数 191 浏览 1 评论 0原文

我正在尝试制作一个应用程序,每天发出一次网络请求,然后向工作管理器推送通知。
现在,我成功地完成了除了工作管理器调度部分之外的所有事情。
我希望它每天早上 8:00 发生一次,但我不知道如何进行。
目前我正在使用 oneTimeWorkRequest,但如果有更好的选择,我会更改它......
那么您认为解决我的问题的最佳方案是什么?

I'm trying to make an app that make a web request once a day and then push a notification with work manager.
Now, I managed to do everything except the part with the scheduling of the workmanager.
I want it to happen once a day at 8:00 am, but I couldn't figure out how.
Currently I'm using oneTimeWorkRequest, but I'll change it if there is a better option...
So what do you think is the best sulotion for my problem?

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

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

发布评论

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

评论(1

生寂 2025-01-23 02:11:17

// 在 onCreate 中你只需调用 startWork();您可以根据需要更改 createConstraints() 中的属性

   private fun createConstraints() = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
    .setRequiresCharging(false)
    .setRequiresBatteryNotLow(true)
    .build()


private fun createWOrkRequest() = PeriodicWorkRequestBuilder<NotifyWorker>(24, TimeUnit.HOURS)
    .setConstraints(createConstraints())
    .setInitialDelay(delay, TimeUnit.MILLISECONDS)

    .build()

private val delay = //TODO - here you have to calculate delay in order to start Worker at desired time. You can

//计算从现在到 8 点之间的时间量(毫秒)

private fun startWork(){
    WorkManager.getInstance(this)
        .enqueueUniquePeriodicWork(
            "Some-Unique-Name",
            ExistingPeriodicWorkPolicy.KEEP,
            createWOrkRequest()
        )
}

在 NotifyWorker 类中您调用 -sendNotification()

class NotifyWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {

override fun doWork(): Result {
    sendNotification()
    return Result.success()
}

private fun sendNotification(){
 // Here you create notification
}
}

这是一种方法,老式方法是使用 AlarmManagers 和 BroadcastReceiver()...

// In onCreate you just call startWork(); you can change attributes in createConstraints() as you want

   private fun createConstraints() = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
    .setRequiresCharging(false)
    .setRequiresBatteryNotLow(true)
    .build()


private fun createWOrkRequest() = PeriodicWorkRequestBuilder<NotifyWorker>(24, TimeUnit.HOURS)
    .setConstraints(createConstraints())
    .setInitialDelay(delay, TimeUnit.MILLISECONDS)

    .build()

private val delay = //TODO - here you have to calculate delay in order to start Worker at desired time. You can

//calculate amount of time (milliseconds) between now and 8 hours 'o clock

private fun startWork(){
    WorkManager.getInstance(this)
        .enqueueUniquePeriodicWork(
            "Some-Unique-Name",
            ExistingPeriodicWorkPolicy.KEEP,
            createWOrkRequest()
        )
}

Inside class NotifyWorker you call -sendNotification()

class NotifyWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {

override fun doWork(): Result {
    sendNotification()
    return Result.success()
}

private fun sendNotification(){
 // Here you create notification
}
}

That is one way to do it, old-fashioned way is to use AlarmManagers and BroadcastReceiver()...

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