如何使用rx java的间隔进行后台任务 [英] How to use rx java's interval for background task

查看:29
本文介绍了如何使用rx java的间隔进行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据https://github.com/ReactiveX/RxAndroid/issues/257#issuecomment-164263215.间隔仅用于活动代码,如果应用程序未唤醒,它将无法运行.那么后台调度任务如何使用interval呢?

According to https://github.com/ReactiveX/RxAndroid/issues/257#issuecomment-164263215 . interval is just for active code, and if app is not wake up, it will not work. So how to use interval for background scheduling tasks?

推荐答案

请不要使用这个解决方案:

Please DO NOT use this solution:

要使用 RxJava 中的 interval,您必须确保应用程序的进程保持活动状态.一种方法是在 Observable>前台服务.这是一个坏主意,因为该服务没有主动向用户提供价值.等待时间过去并不能为用户带来价值.再次请不要使用这个.

To use interval from RxJava you'll have to make sure your app's process stays alive. One way to do it is to put use the Observable in a foreground service. This is a bad idea because the service is NOT actively delivering value to the user. Waiting for time to pass is not delivering value for the user. Again please DO NOT use this.

AlarmManagerJobScheduler(或者它的backport GcmNetworkManager) 是重复后台活动的更好选择.如果您使用 AlarmManager.setInexactRepeating(),系统可以将多个应用程序的作业一起批处理以节省电池电量.使用 JobScheduler 使您能够在特定条件下执行后台作业,例如.当设备连接到互联网或电池电量超过 20% 时.(需要上网查天气).

AlarmManager and JobScheduler (or it's backport GcmNetworkManager) are far better choices for repeating background activities. If you use AlarmManager.setInexactRepeating() the system can batch jobs from multiple apps together to save battery. Using JobScheduler enables you to execute your background jobs in specific conditions, eg. when the device is connected to the internet or when battery is more than 20%. (Internet is required to check the weather).

interval 来自 RxJava 确实在 Android 上有它的用法.对于相对较短的持续时间,它是 Runnable.postDelayed 的绝佳替代品.它使代码更短,更具可读性.

interval from RxJava does have it's usage on Android. It's an excellent replacement for Runnable.postDelayed for relatively short durations. It makes the code shorter and more readable.

这篇关于如何使用rx java的间隔进行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆