Android的服务:onBind(意向)和onUnbind(意向)被调用一次 [英] Android Service: onBind(Intent) and onUnbind(Intent) is called just once

查看:1669
本文介绍了Android的服务:onBind(意向)和onUnbind(意向)被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动和服务。当活动开始时,它会调用 startService()来使这个服务是活着的,即使活动被破坏, bindService(),与该服务进行通信。

I have the Activity and the Service. When Activity is started, it calls startService() to make this Service be alive even when Activity is destroyed, and bindService(), to communicate with this Service.

bindService()返回true, mService.onBind()被调用,而 ServiceConnection.onServiceConnected()叫了。所有的作品。

bindService() returns true, mService.onBind() is called, and ServiceConnection.onServiceConnected() is called too. All works.

当我毁了我的活动由pressing返回键,它会调用 unbindService(),和我的 mService.onUnbind()被调用。 (我返回在此 onUnbind()

When i destroy my Activity by pressing Back key, it calls unbindService(), and my mService.onUnbind() is called. (i return false in this onUnbind().)

然后我重新开始这个活动, bindService()返回true,然后 mService.onBind()不会叫!但 ServiceConnection.onServiceConnected()叫过,并再次一切工作。

Then i start this Activity again, bindService() returns true, and then mService.onBind() is NOT called! But ServiceConnection.onServiceConnected() is called too, and all works again.

看起来的Dalvik会记住我的 onBind()返回上一次,只是不叫 onBind()一次。当我再次毁了我的活动, onUnbind()不会叫了。

It looks like Dalvik remembers what my onBind() returned last time, and just does not call onBind() again. When i destroy my Activity again, onUnbind() is NOT called too.

我可以绑定和取消绑定这个服务我的活动任意次数,但这些方法将不再被调用,直到我毁灭服务被解除绑定和调用 stopService()

I can bind and unbind this Service to my Activity any number of times, but these methods will not be called anymore until I destroy Service by unbinding and calling stopService().

在文档我找不到这种行为作出任何解释。相反,这个数字显示, onBind() onUnbind()应该叫每一次客户端绑定和取消绑定服务。这个数字可以在这个开发指南。

In docs i can't find any explanation of this behavior. Conversely, this figure shows that onBind() and onUnbind() should be called every time clients bind and unbind Service. This figure can be found on the bottom of this Dev Guide.

推荐答案

我觉得(官方开发指南引用的)可以解释所有的疑问:

I think this (referenced from official dev guide) can explain all your queries:

多个客户端可以连接到服务一次。但是,系统调用服务的onBind()方法只检索的IBinder当第一个客户端绑定。然后,系统将提供相同的IBinder到绑定任何其他客户端,而不调用onBind()了。

Multiple clients can connect to the service at once. However, the system calls your service's onBind() method to retrieve the IBinder only when the first client binds. The system then delivers the same IBinder to any additional clients that bind, without calling onBind() again.

这篇关于Android的服务:onBind(意向)和onUnbind(意向)被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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