ServiceConnection在Android中泄漏

我试图在Android中使用一些服务来进行一些基本的数据库操作,但由于某种原因,我得到一个Activity泄漏了ServiceConnection错误。 我将在底部发布完整的Logcat读数。

我必须在多个活动中使用相同的服务,所以我创build了一个超类来处理所有的服务任务。 它看起来像这样:

private MyInterface child; public void onCreate(Bundle savedInstanceState, MyInterface child){ super.onCreate(savedInstanceState); doBindService(); } public void onResume(){ super.onResume(); doBindService(); } protected void onPause(){ super.onPause(); doUnbindService(); } private boolean bound; private boolean binding; ServiceConnection Connection = new ServiceConnection(){ //called when the service is connected @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(LOGTAG, "Bound to Service"); bound = true; binding = false; toServiceMessenger = new Messenger(service); while(!commandsForService.isEmpty()){ sendToService(commandsForService.poll()); } } //called when the service is disconnected @Override public void onServiceDisconnected(ComponentName name) { Log.d(LOGTAG, "Unboud from Service"); bound = false; binding = false; toServiceMessenger = null; } }; private boolean doBindService(){ Log.d(LOGTAG, "Attempting to Bind to Service"); if(!bound && !binding){ binding = true; bindService(new Intent(this, global.FetchService.class), Connection, Context.BIND_AUTO_CREATE); } return bound; } private void doUnbindService(){ Log.d(LOGTAG, "Attempting to Unbind from Service"); if(bound && !binding){ binding = true; unbindService(Connection); } } public void sendToService(Message msg){ if(bound){ sendMessageToService(msg); } else{ commandsForService.add(msg); } } private void sendMessageToService(Message msg){ if(bound){ msg.replyTo = fromServiceMessenger; try { toServiceMessenger.send(msg); } catch (RemoteException e) { Log.d(LOGTAG, "RemoteException communicating with service"); } } else{ Log.d(LOGTAG, "Error: toServiceMessenger null while bound"); } } 

这个想法是,孩子的活动永远不需要担心连接到服务,超类应该照顾获取数据的服务,并返回给孩子。

Logcat指向onCreate() – > bindService(new Intent(this,global.FetchService.class),Connection,Context.BIND_AUTO_CREATE)中的doBindService() 作为导致错误的线路。 但是,该服务只有在活动运行后才会泄漏,并且可见时间超过15秒,所以我不认为应该调用onCreate()。

这里是Logcat:

 09-02 09:25:40.635: E/ActivityThread(5963): Activity childActivity has leaked ServiceConnection superClass$1@42cb1b70 that was originally bound here 09-02 09:25:40.635: E/ActivityThread(5963): android.app.ServiceConnectionLeaked: Activity childActivity has leaked ServiceConnection superClass$1@42cb1b70 that was originally bound here 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1055) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:949) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ContextImpl.bindService(ContextImpl.java:1472) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ContextImpl.bindService(ContextImpl.java:1464) 09-02 09:25:40.635: E/ActivityThread(5963): at android.content.ContextWrapper.bindService(ContextWrapper.java:394) 09-02 09:25:40.635: E/ActivityThread(5963): at superClass.doBindService(FetchActivity.java:253) 09-02 09:25:40.635: E/ActivityThread(5963): at superClass.onCreate(FetchActivity.java:61) 09-02 09:25:40.635: E/ActivityThread(5963): at childActivity.onCreate(Showcase_Activity.java:37) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.Activity.performCreate(Activity.java:5066) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ActivityThread.access$600(ActivityThread.java:151) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335) 09-02 09:25:40.635: E/ActivityThread(5963): at android.os.Handler.dispatchMessage(Handler.java:99) 09-02 09:25:40.635: E/ActivityThread(5963): at android.os.Looper.loop(Looper.java:155) 09-02 09:25:40.635: E/ActivityThread(5963): at android.app.ActivityThread.main(ActivityThread.java:5493) 09-02 09:25:40.635: E/ActivityThread(5963): at java.lang.reflect.Method.invokeNative(Native Method) 09-02 09:25:40.635: E/ActivityThread(5963): at java.lang.reflect.Method.invoke(Method.java:511) 09-02 09:25:40.635: E/ActivityThread(5963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 09-02 09:25:40.635: E/ActivityThread(5963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) 09-02 09:25:40.635: E/ActivityThread(5963): at dalvik.system.NativeStart.main(Native Method) 

任何帮助将不胜感激。

Solutions Collecting From Web of "ServiceConnection在Android中泄漏"

当您在onCreate()调用doBindService()时,您应该在onDestroy()添加do UnbindService() onDestroy()

这个对我有用。

你在onCreate()onResume()调用了doUnbindService() ,只是在onResume()调用它,以便将你的调用与onPause() doUnbindService() onPause()

您需要使用活动getActivity()的良好引用返回当前的引用,并且您需要解除绑定的绑定

这里是我如何解决这个问题:

 private Activity mActivity; @Override public void onStart() { super.onStart(); if (!mBound) { startService(); mActivity = getActivity(); } } @Override public void onStop() { super.onStop(); if (mBound && mActivity != null) { mActivity.unbindService(mServiceConnection); mActivity = null; } } 

您遇到此错误是因为您绑定活动的服务在该活动被销毁时不会停止。 所以由此你的服务仍然存在于内存和消耗资源,这是无所事事。

为了解决这个问题,调用onCreate() doUnbindService()onDestroy() doUnbindService() onDestroy()