Android 服务连接泄漏是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12308400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to leak a service connection?
提问by Teovald
I am writing a service for my Android app and I am trying to understand how the binding mechanism works. If I bind my service in the onCreate of an activity but I don't unbind it in onStop or onDestroy, I get the error :
我正在为我的 Android 应用程序编写一个服务,我试图了解绑定机制是如何工作的。如果我在活动的 onCreate 中绑定我的服务,但我没有在 onStop 或 onDestroy 中取消绑定,我会收到错误消息:
android.app.ServiceConnectionLeaked: Service com.google.ipc.invalidation.ticl.android.AndroidInvalidationService has leaked ServiceConnection com.googl
e.ipc.invalidation.external.client.android.service.ServiceBinder@4177f8f8 that was originally bound here
So my question is : what is the problem exactly with leaking a connection, what am I preventing by unbinding my service ?
所以我的问题是:泄漏连接到底有什么问题,我通过解除绑定我的服务来防止什么?
回答by Sam
Typically your app is the only app that can access your Service (or have a use for it). If destroy your app with this Service still running then that Service no longer has a purpose. It will exist in memory consuming resources but doing nothing. By unbinding or stopping your Service when you are done, you return these resources so that they can be used by other apps.
通常,您的应用程序是唯一可以访问您的服务(或使用它)的应用程序。如果在此服务仍在运行的情况下销毁您的应用程序,则该服务不再具有用途。它将存在于消耗资源的内存中,但什么也不做。通过在完成后解除绑定或停止您的服务,您将返回这些资源,以便其他应用程序可以使用它们。
Think of a child that spreads their toys across a room and then leaves. Everyone else in the house (the parents, siblings and guests) has no use for them and must walk around everything. The toys are simply in the way. If the child puts them away, then anyone can use the room for whatever they want.
想想一个孩子把他们的玩具摊在一个房间里然后离开。房子里的其他人(父母、兄弟姐妹和客人)对他们毫无用处,必须四处走动。玩具只是碍手碍脚。如果孩子把它们收起来,那么任何人都可以随意使用房间。
Hope that helps!
希望有帮助!
回答by Mohammad Ersan
That happens when Android OS destroys Activity
and finds that a ServiceConnection
still bound to a running Service
, so you need to unbind the service before destroying your Activity
当 Android 操作系统销毁Activity
并发现 aServiceConnection
仍然绑定到运行时会发生这种情况Service
,因此您需要在销毁您的服务之前解除绑定服务Activity
回答by Vishruit Kulshreshtha
The services are still running even after the activity gets closed. So you need to unbind the resource in onStop or onDestroy.
即使活动关闭后,服务仍在运行。所以你需要在onStop或onDestroy解绑资源。