安卓:辛格尔顿这是用来活动和服务之间 [英] Android: Singleton which is used between Activity and Service

查看:107
本文介绍了安卓:辛格尔顿这是用来活动和服务之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否这将是一个坏主意,创建用于Android的一些活动和Android的服务之间的辛格尔顿。据我所知道的静态字段,在我的情况辛格尔顿,可只要整个过程是活的。

im wondering if it would be a bad idea to create a Singleton that is used between some Android Activities and a Android Service. As far as I know the static fields, in my case the Singleton, is available as long as the whole Process is alive.

我的计划是使用一个单身,而不是Parcelable到我的活动和后台服务之间共享数据。因此,我的活动1将通过调用MySingleton.getInstance()添加一些数据addData(富)。然后我就发了意向通知我的新数据已添加到单服务。接下来我BackgroundService会处理这个意图,并呼吁MySingleton.getInstance()getLatestData();然后,将处理的数据(需要一些时间)。该服务的结果将是下一个后回使用单和消防广播的意图,这是由活性1(如果活着)及相关活动,处理将检索单的结果。

My plan is to use a singleton instead of Parcelable to share data between my activities and a Background service. So my Activity1 will add some data by calling MySingleton.getInstance().addData(foo); then I would sent an Intent to inform my Service that new Data has been added to the singleton. Next my BackgroundService would handle the intent and call MySingleton.getInstance().getLatestData(); then it would process the data (takes some time). The result of the service would next be "post" back by using the singleton and fire a broadcast intent, which are handled by the Activity1 (if alive) and the Activity1 will retrieve the result from the singleton.

你们觉得这就是一个坏主意?

Do you guys think thats a bad idea?

编辑: 我想实现是软件的一个和平,从Web服务器下载数据分析它并返回结果。因此,我的活动将创造DownloadJob对象。该DownloadJob-对象将被放入DownloadScheduler(单身)的队列,并管理所有DownloadJobs。所述DownloadScheduler将使运行5 DownloadJobs同时并使用队列来存储的等待。有效下载将由下载服务(IntentService),其被告知在一个Intent的一个新的DownloadJob现在应该被执行(下载)现在来完成。该DowanlodService将检索来自DownloadSchedulers队列(的PriorityBlockingQueue)接下来的工作,并通过设置DownloadJob.setResult(...)返回结果,并触发了一个广播的意图,这个结果已经准备就绪,将由DownloadScheduler收到这将remve从队列作业,并通知下载完成等活动。

What I want to implement is an peace of software that downloads data from a web server parse it and return the result. So my Activity would create DownloadJob Object. The DownloadJob-Object would be put into the DownloadScheduler (Singleton) which queues and manage all DownloadJobs. The DownloadScheduler would allow to run 5 DownloadJobs at the same time and use a queue to store the waiting. The effective Download would be done by the DownloadService (IntentService), which gets informed over an Intent that the a new DownloadJob should now be executed (downloaded) right now. The DowanlodService would retrieve the next job from the DownloadSchedulers queue (PriorityBlockingQueue) and return the Result by setting DownloadJob.setResult(...) and fires up an broadcast intent, that the Result is ready, which will be received by the DownloadScheduler which would remve the job from the queue and inform the Activity that the download is complete etc.

因此​​,在我的情况下我会使用单从下载服务,而不是做一个DownloadJob Parcelable访问DownloadJobs并与意图通过。所以,我会避免这个问题,我有在内存中的两个DownloadJobs(一个对活动现场,一个在服务网站)。

So in my scenario I would use the singleton to access the DownloadJobs from the DownloadService instead of making a DownloadJob Parcelable and pass it with the Intent. So i would avoid the problem, that I have two DownloadJobs in memory (one on the "Activity Site" and one on "Service site").

任何建议如何解决这更好的?

Any suggestions how to solve this better?

这是真的,静态情况下,像DownloadScheduler(单身),将用于释放了低内存的Andr​​oid系统?因此,将继承的应用程序,并认为不存在参考(非静态)避免这个问题?

Is it true that static instances, like DownloadScheduler(Singleton), would be used by freed by the android system on low memory? So would subclassing the Application and hold there the reference (non static) avoid this problem?

推荐答案

如果您使用的是单身的只是的作为后台服务之间共享内存,我认为在不同的线程执行操作,你可能会遇到同步问题,或读取数据不一致。

If you are using the singleton just as shared memory between a background service which I assume is performing operations on a different thread, you may run into synchronization issues and or read inconsistent data.

如果在单身的数据不同步,你必须要小心,因为你是靠你的协议,以确保没有人,而你的后台线程写入(这可能会导致错误)阅读。

If the data in the singleton is not synchronized, you have to be careful because you are relying on your "protocol" to be sure that nobody is reading while your background thread is writing (which may lead to errors).

在另一方面,如果它是同步的,则是冒着面对ANR错误,因为它读取数据时,活性可以阻塞等待服务完成写在单中的数据。

On the other hand, if it is synchronized, you are risking to face anr error because the activity which reads the data may be blocked waiting the service to finish to write the data in the singleton.

由于对方说,你还必须记住,你的单可能被释放,如果操作系统需要的资源,你的数据可能不存在了。

As the other said, you also have to keep in mind that your singleton may be freed if the os needs resources, and that your data may not be there anymore.

我宁愿使用一个事件总线,如奥托或的 eventbus

I'd rather use an event bus such as otto or eventbus

编辑:

使用单作为背景(意向)的入口点服务是在2010年维吉尔Dobjanschi的如何构建REST客户端应用程序为Android。

Using a singleton as the entry point of background (intent) service is the approach suggested in 2010 Virgil Dobjanschi talk about building rest client applications for android.

建议的方法是有一个单执行正在进行的请求控制器。请还认为,请求服务的意图已经排队由操作系统,这样你就可以抛出,将被意图的服务顺序进行处理几个意向。

The suggested approach is having a singleton that performs as controller of ongoing requests. Please consider also that request to intent service are already queued by the os, so you can throw several intents that will be processed sequentially by the intent service.

前一段时间我也试过把它看作一个起点,一个图书馆,它仍然未完成。你可以找到源头这里

Some time ago I also tried take that as a starting point for a library, which still remains unfinished. YOu can find the sources here

我肯定不会做的,是存储数据的单。我想preFER的方法是存储在某些持久性存储中的数据(如SQL / preferences /文件/内容提供商),并让客户知道通过广播消息的改变(或者,如果你是使用内容提供者,通过观察者)。

What I would certainly not do is to store your data in the singleton. The approach I would prefer is to store the data in some persistent storage (such as sql / preferences / file / content provider) and let the client know of the change through a broadcast message (or, if you are using a content provider, through an observer).

最后,在一定程度上,这是方法其次 robospice 库,它看起来相当成熟和船舶很多有趣的功能,如高速缓存。

Finally, to some extent this is the approach followed by the robospice library, which looks quite mature and ships a lot of interesting features such as caching.

这篇关于安卓:辛格尔顿这是用来活动和服务之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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