我如何处理完成工作的工作?

发布于 2025-01-20 20:23:50 字数 1307 浏览 0 评论 0原文

大家好。正如标题所示,我需要在 WorkManager 完成工作时(每 60 秒)抽出时间来执行其他操作。目前我知道我不能用它来完成少于 15 分钟的工作,但通过尝试一些不同的方法我可以解决这个问题。但事实仍然是,我需要弄清楚 OneTimeWorkRequest 命令计时器何时结束。你有什么建议给我? 这是我的代码:

MainActivity:

    class MAinActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setPeriodicallySendingLogs()
    }

    private fun setPeriodicallySendingLogs() {
        val workManager = WorkManager.getInstance(this)
        val sendingLog = PeriodicWorkRequestBuilder<SendLogWorker>(15, TimeUnit.MINUTES)
            .build()

        workManager.enqueue(sendingLog)

        workManager.getWorkInfoByIdLiveData(sendingLog.id)
            .observe(this, Observer { workInfo ->
                Log.d("WM", " info${workInfo.state.name}")
            })
    }
}

Worker:

class SendLogWorker(private val context: Context, userParameters: WorkerParameters) :
    Worker(context, userParameters) {

    override fun doWork(): Result {
        val mywork = OneTimeWorkRequest.Builder(SendLogWorker::class.java)
            .setInitialDelay(1, TimeUnit.MINUTES)
            .build()
        WorkManager.getInstance(context).enqueue(mywork)

        return Result.success()
    }
}

Hi everyone. As the title suggests, I need to take the time when WorkManager finishes its work (every 60 seconds) to perform other operations. At the moment I know that I can't use it for jobs less than 15 minutes, but by trying some different way I can get around the problem. But the fact remains that I need to figure out when the OneTimeWorkRequest command timer ends. What do you suggest to me?
Here my code:

MainActivity:

    class MAinActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setPeriodicallySendingLogs()
    }

    private fun setPeriodicallySendingLogs() {
        val workManager = WorkManager.getInstance(this)
        val sendingLog = PeriodicWorkRequestBuilder<SendLogWorker>(15, TimeUnit.MINUTES)
            .build()

        workManager.enqueue(sendingLog)

        workManager.getWorkInfoByIdLiveData(sendingLog.id)
            .observe(this, Observer { workInfo ->
                Log.d("WM", " info${workInfo.state.name}")
            })
    }
}

Worker:

class SendLogWorker(private val context: Context, userParameters: WorkerParameters) :
    Worker(context, userParameters) {

    override fun doWork(): Result {
        val mywork = OneTimeWorkRequest.Builder(SendLogWorker::class.java)
            .setInitialDelay(1, TimeUnit.MINUTES)
            .build()
        WorkManager.getInstance(context).enqueue(mywork)

        return Result.success()
    }
}

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

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

发布评论

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

评论(1

So尛奶瓶 2025-01-27 20:23:50
workManager.getWorkInfoByIdLiveData(syncWorker.id)
        .observe(getViewLifecycleOwner(), workInfo -> {
    if (workInfo.getState() != null &&
            workInfo.getState() == WorkInfo.State.SUCCEEDED) {
        Snackbar.make(requireView(),
                    R.string.work_completed, Snackbar.LENGTH_SHORT)
                .show();
   }
});

https://developer.android.com/topic/库/架构/workmanager/如何/管理工作

workManager.getWorkInfoByIdLiveData(syncWorker.id)
        .observe(getViewLifecycleOwner(), workInfo -> {
    if (workInfo.getState() != null &&
            workInfo.getState() == WorkInfo.State.SUCCEEDED) {
        Snackbar.make(requireView(),
                    R.string.work_completed, Snackbar.LENGTH_SHORT)
                .show();
   }
});

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/managing-work

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