Robospice - 保持香料业务不断变化的活动时运行 [英] Robospice - keep spice service continue running when changing activity

查看:220
本文介绍了Robospice - 保持香料业务不断变化的活动时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 RoboSpice 库去做异步任务。从它们的实施​​例中,香料服务是在BaseActivity声明,它开始在活动的ONSTART方法,并在活动的的onStop停止。一切都很好,但是当我想下载从互联网上的文件,然后我换到另一个活动,这个下载任务被取消,因为香料服务已停止。它看起来像:

I used RoboSpice library to do asynchronous tasks. From their examples, the spice service is declared in BaseActivity, it starts in activity's onStart method, and stops in activity's onStop. Everything is fine, but when I want download file from internet, and then I change to another activity, this download task is cancelled, because the spice service is stopped. It looks like:

public abstract class BaseActivity{
  /** The request manager. */
private SpiceManager requestManager = new SpiceManager(RequestService.class);   
  @Override
   protected void onStart() {   
      requestManager.start(this);
      super.onStart();     

   }

  @Override
  protected void onStop() {
    requestManager.shouldStop();
    super.onStop();
  }
} 

所以,我想知道有没有什么安全的方式,让下载任务继续从香料服务运行(此任务不碰UI),而另一个任务正常工作(意味着他们可以被取消时停止活动),但它还是可以的尊重生命活动圈子。

So I wonder is there any safe way to keep the download task continue running from Spice service (this task doesn't touch UI), and another tasks work normally (mean they can be cancelled when stop activity) but it still can respect the activity life circle.

推荐答案

@ R4J,你错了。当活动举办spicemanager停止请求不会停止。在RS,请求将有自己的生命周期,在不同的上下文(香料的服务,是一个Android服务)将被exectued。

@R4j, you missed the point. Requests are not stopped when the activity holding the spicemanager stops. In RS, requests will have their own lifecycle and will be exectued in a different context (a spice service that is an Android Service).

因此​​,如果你开除的要求,那么你的活动死亡或者杀害被,香料请求将继续以自己的方式和下载将继续进行。会发生以及解析,最后你的结果将被放置在缓存中。如果有的话。

Thus, if you fire a request, then your activity dies or gets killed, the spice request will go on its own way and download will continue. Parsing will occur as well, and finally your result will be placed in the cache. If any.

所以,如果你想触发活性的请求并得到结果的活动B,你应该:

So if you want to trigger a request in activity A and get the result in activity B, you should :

  • 在简单地执行活动中的一个如常的要求,遵循RS样本
  • 在B,开始你的spiceManager后(即右后super.onStart()),做两件事情:

  • simply execute the request in activity A as usual, follow RS samples
  • in B, after starting your spiceManager (i.e. right after super.onStart()), do 2 things :

  • 使用spiceManager.addListenerIfPending来重新插入新的活动的任何未处理的请求
  • 使用spiceManager.getDataFromCache得到可能已经被放入缓存由previous请求的任何结果

optionnally,如果要重新执行B中本身,你可以请求,你会很高兴地得知,如果使用相同的请求类相同的缓存键,RS将聚集新请求你任何未处理的请求。

optionnally, if you want to re-execute the request in B itself, you can, and you will be happy to learn that if you use the same request class with the same cache key, RS will aggregate your new request to any pending request for you.

在RS,请求标识由制造类的请求的结果+请求缓存的复合键。此化合物密钥可以用于检索把通过在高速缓存此请求挂起申请以及任何结果

In RS, a request is identified by a compound key made of "class of result of a request" + "request cache". This compound key can be used to retrieve a pending request as well as any result put by this request in the cache.

这篇关于Robospice - 保持香料业务不断变化的活动时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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