绑定到从从应用程序上下文的配置更改或绑定新的上下文服务?上下文、绑定、应用程序

2023-09-05 02:46:28 作者:思念是壹種煎熬

我想工作,如果绑定服务是否适合做后台工作在我的应用程序。的要求是,各种应用组件可以通过它使不同的优先的Web请求。 (因此,服务必须保持某种队列并能够取消它对于较高优先级的其它正在进行的请求)。我想的服务是相对不显眼的用户,使得他们没有发现它运行后,他们完成的应用程序 - 如果我想这样做,继续当应用程序被关闭,我可以用startForeground(更重要的东西),以在过程中按一个通知。

I'm trying to work out if bound service is appropriate for doing background work in my app. The requirements are that various application components can make web requests through it of varying priority. (Thus the service must maintain some sort of queue and be able to cancel it's ongoing requests for others of higher priority). I'd like the service to be relatively unobtrusive to the user such that they don't find it running after they are done with the application - if I want to do something more important that continues while the application is closed I can use startForeground() to push a notification during the process.

解决方案第一:从绑定的活性

因此​​,对于给定的应用程序组件应该是能够结合到完成工作的服务。但似乎有一个众所周知的问题,即如果一个活动被执行绑定,绑定期间,将配置改变(旋转)作为活动将被关闭丢失。

So, for a given application component it should be able to bind to the service to get work done. But there seems to be a well known problem that if an activity is doing the binding, the binding will be lost during configuration change (rotation) as the activity will be closed.

所以,我想我可以用我创建另一个上下文(新的上下文()),并结合从该服务,然后使用非UI片段直到我认为,我完成了它保持整个配置改变这样的背景下。我只能在配置改变或作为永久的替代方案,从活动的结合做到这一点。 (我也许应该指出,这是一个标准的推荐的方式来保持整个配置的变化情况)

So, I was thinking I could use another context that I create (new Context()) and bind from that to the service, then use a non-UI fragment to maintain this context across config changes until I deem that I am finished with it. I could do this only during the configuration change or as a permanent alternative to binding from the activity. (I should probably point out that this is a standard and recommended way to maintain instances across config changes)

解决方案NUMERO 2:

我看到的主要选择是,我可以使用应用程序上下文做绑定 - 但会这样持续太久?和/或可能有应用程序范围内,因此preventing服务和应用程序环境被破坏的服务之间的一些循环关系?

The main alternative I see is that I can use the application context to do the binding - but could this persist too long? and/or could there be some cyclic relationship between the app context and the service thus preventing the service and the app context being destroyed?

问题:

所以这个问题我想回答自己的是:我应该使用第一种方法(活动临时环境)?或第二(刚刚绑定的服务应用程序上下文)?

So the question I'm trying to answer to myself is: should I use the first method (activities with temporary contexts)? Or the second (just bind service to the app context)?

我是正确的思维应用程序上下文可以绑定到服务多次,然后从它解除了相同的次数? (也就是说,你可以为每个上下文多个有效绑定)?

Am I right in thinking the app context can bind to the service multiple times and then unbind from it the same number of times? (I.e. that you can have multiple valid bindings PER context)?

可以用我自己的上下文(新的上下文())中第一个解决方案造成任何问题?

Could using my own context (new Context()) in the first solution cause any issues?

修改

找到一些信息: https://groups.google.com /论坛/#!话题/ Android的开发者/ Nb58dOQ8Xfw

有也似乎它将难以创造上下文任意这样的溶液的组合1和2似乎是适当的,其中业务连接被维持整个结构改变,但结合是该应用的上下文。我仍然担心从应用程序上下文解除绑定两次的可能性。保持绑定计数自己似乎没有必要 - ?任何人能确认/否认绑定每个连接而不是每个方面

It also seems that it will difficult to 'create' a context arbitrarily so a combination of solution 1 and 2 seems appropriate where the service connection is maintained across the configurations change but the binding is to the app context. I am still concerned about the possibility of unbinding twice from the app context. Keeping count of the bindings myself seems unnecessary - can anyone confirm/deny that bindings are per connection and not per context?

推荐答案

所以,做一些挖我想我已经提出了一个(尚未)未经测试的解决方案后。

So after doing some digging I think I have come up with an (as yet) untested solution.

首先,根据戴安娜的建议在这里: https://开头组。 google.com/forum/#​​!topic/android-developers/Nb58dOQ8Xfw 我应该结合到应用程序上下文 - 因此失去的情况下我的问题了 - 我可以在配置改变与非维护我ServiceConnection UI片段 - 伟大的。然后,当我做了我可以使用应用程序上下文交还服务连接和取消绑定。我不应该接受任何渗漏服务连接警告。 (我也许应该指出,这是一个标准的推荐的方式来保持整个配置的变化情况)

Firstly, based on Diane's suggestion here: https://groups.google.com/forum/#!topic/android-developers/Nb58dOQ8Xfw I should be binding to the application context - so my problem of losing the context is gone - I can maintain my ServiceConnection across configuration changed with a Non-UI fragment - great. Then when I am done I can use the app context to hand back the service connection and unbind. I shouldn't receive any leaky service connection warnings. (I should probably point out that this is a standard and recommended way to maintain instances across config changes)

这个问题的最终症结是我不确定我是否可以绑定多个次来自同一背景下 - 上绑定的单证意味着有绑定和上下文的生命周期,所以我很担心我会之间的一些依赖做我自己的引用计数形式。我有一个看看源$ C ​​$ C,结束了这里:http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/app/LoadedApk.java#LoadedApk.forgetServiceDispatcher%28android.content.Context%2Candroid.content.ServiceConnection%29

The final crux of this problem was I was unsure of whether I could bind multiple times from the same context - the documentations on bindings imply there is some dependence between the binding and the context's lifecycle and so I was worried I would have to do my own form of reference counting. I had a look at the source code and ended up here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/app/LoadedApk.java#LoadedApk.forgetServiceDispatcher%28android.content.Context%2Candroid.content.ServiceConnection%29

最重要的是,这些行:

sd = map.get(c);
    if (sd != null) {
        map.remove(c);
        sd.doForget();
        if (map.size() == 0) {
            mServices.remove(context);
        }

显示了地图被用于引用计数我很担心。

Reveal that the map is being used for the reference counting I was worried about.

所以带回家是这样的:

绑定服务将正常工作与应用程序方面,我们应该这样做,以prevent泄露从一个活动一个配置变化过程中的服务连接到另一 我可以保持在一个非UI片段,我的服务连接安全地并用它来解除绑定,当我做了

我会尝试和发布一些测试code很快。

I'll try and post some tested code soon.

更新和测试的解决方案:我做了一些code,以测试这一点,并发表在这里:的 https://github.com/samskiter/BoundServiceTest

UPDATE and tested solution: I've made some code to test this and published here: https://github.com/samskiter/BoundServiceTest

这似乎运作得很好,非UI片段(数据片段)作为一个很好的代理侦听器中旋转的变化,从服务(听众的意图赶上结果是紧密绑定请求到UI中为了保证它保持敏感。显然,任何模式的变化可以传播到通过观察用户界面。)

It seems to work quite well and the non-ui fragment (data fragment) acts as a nice proxy listener during rotation changes to catch results from the service (the intention of the listeners is to closely bind the requests to the UI in order to guarantee it stays responsive. Obviously any model changes can be propagated to the UI via observers.)

编辑:我想我应该明确地回答了OP的问题...

I thought I should explicitly answer the questions in the OP...

我应该使用第一种方法(活动临时环境)?或第二(刚刚绑定的服务应用程序上下文)? 第二

我是正确的思维应用程序上下文可以绑定到服务多次,然后从它解除了相同的次数? (也就是说,你可以为每个上下文多个有效绑定)? 是

Am I right in thinking the app context can bind to the service multiple times and then unbind from it the same number of times? (I.e. that you can have multiple valid bindings PER context)? Yes

可以用我自己的第一个解决方案上下文(新的上下文())造成任何问题? 这甚至是不可能

Could using my own context (new Context()) in the first solution cause any issues? This is not even possible

有一个最后的总结:

这个模式应该是pretty的强大 - 我可以优先网络IO(或其他任务)从各种跨我的应用程序源的。我可以有一个前台活动做一些小的IO用户要求,同时我可以踢前台服务同步我的所有用户数据。两者foregrounds服务和活性可以绑定到同一网络服务获得他们的请求进行。

This pattern should be pretty powerful - I can prioritise network IO (or other tasks) coming from a variety of sources across my app. I could have a foreground activity making some small io the user has asked for, simultaneously I could have kicked of a foreground service to sync all my users data. Both the foregrounds service and the activity can be bound to the same Network service to get their requests done.

这一切,同时确保服务只生活完全一样,只要它需要 - 即它很好地发挥了Android

All this while making sure the service lives only exactly as long as it needs to - i.e. it plays nicely with android.

我很高兴能进入一个应用程序,这很快。

I'm excited to get this into an app soon.

更新:我试着写了并给予一定的上下文背景的工作更广泛的问题,在博客中的位置: http://blog.airsource.co.uk/2014/09/10/android-bound-services/

UPDATE: I've tried to write this up and give some context to the wider problem of background work in a blog entry here: http://blog.airsource.co.uk/2014/09/10/android-bound-services/