将JobIntentService绑定到Activity [英] Binding a JobIntentService to a Activity

查看:393
本文介绍了将JobIntentService绑定到Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中运行一个长时间运行的操作。说任务将持续约5-10分钟。出于这个原因,我打算使用 JobIntentService 并将其绑定到活动

I want to run an long running operation in Android. Say the task would run for about 5-10 mins. For this reason I am planning to use a JobIntentService and Bind it to an Activity.

现在我正在使用 AsyncTask ,即使我知道 AsyncTask 也不能/应该不能用于长时间运行,因此我打算现在改变它。很多时候发生的事情是在任务运行时用户最小化应用程序,之后Android操作系统关闭/清除活动以释放一些内存。

Right now I am using a AsyncTask, even though I know AsyncTask cannot/should not be used for long running operations hence I am planning to change it now. Many times what happens is while the task is running the user minimizes the app and after sometime the Android OS closes/clears the Activity to free up some memory.

所以我的 AsyncTask 在尝试更新<$ c中的视图时保持运行没有任何目的和崩溃$ c>活动。

所以我打算使用 JobIntentService 。但是使用 JobIntentService 并将其绑定到活动将减少Android操作系统关闭/清除<$的机会C $ C>活性?或者它仍然会遵循相同的过程吗?

So I am planning to use an JobIntentService . But will using an JobIntentService and Binding it to an Activity will reduce the chances of Android OS closing/clearing the Activity? or still will it follow the same process?

任何帮助都会非常感激。

Any Help would be really grateful.

推荐答案

如果你的活动在后台,那么阻止Android杀死你的活动就听不到了对我来说是一个好主意(甚至是必要的)。由于您的活动在后台,因此用户无法看到,并且在此期间无需更新UI。

If your Activity is in the background then blocking Android from killing your Activity doesn't sound like a good idea (and even necessary) to me. Since your Activity is in the background it's not visible to the user and there's no need to update the UI during this time.

相反,您可以执行以下操作:

Instead, you could do the following:

如果活动位于前台,然后它可以通过 BroadcastReceiver JobInceService 接收更新,您可以使用<注册/取消注册 LocalBroadcastManager in onResume / onPause

If Activity is in the foreground then it can receive updates from your JobIntentService via BroadcastReceiver that you register/unregister with LocalBroadcastManager in onResume/onPause.

IF JobIntentService 活动时已完成工作被杀或在后台你可以将结果保存在某处(例如,在 SharedPreferences 中),然后在 onResume 中您的活动您可以从 SharedPreferences 中读取该结果。

IF JobIntentService has completed its work when your Activity was killed or in the background you could persist the result somewhere (for example, in SharedPreferences) and then in onResume of your Activity you could read that result from SharedPreferences.

这篇关于将JobIntentService绑定到Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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