java android.app.ServiceConnectionLeaked: Activity ...MainActivity 泄露了原本绑定在这里的ServiceConnection ...MainActivity$1@e794142
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35603541/
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
android.app.ServiceConnectionLeaked: Activity ...MainActivity has leaked ServiceConnection ...MainActivity$1@e794142 that was originally bound here
提问by Bharat
its failing @ below line -
它在下面的失败@ -
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
Below is the trace....
下面是轨迹....
Activity com.example.alwaysrunningprocesswithcallanswertap.MainActivity has leaked ServiceConnection com.example.alwaysrunningprocesswithcallanswertap.MainActivity@e794142 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.alwaysrunningprocesswithcallanswertap.MainActivity has leaked ServiceConnection com.example.alwaysrunningprocesswithcallanswertap.MainActivity@e794142 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1077)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:971)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1774)
at android.app.ContextImpl.bindService(ContextImpl.java:1757)
at android.content.ContextWrapper.bindService(ContextWrapper.java:539)
at com.example.alwaysrunningprocesswithcallanswertap.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
MainActivity.class
主Activity.class
public class MainActivity extends Activity
{
CallNotifierService m_service;
boolean isBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
m_service = ((CallNotifierService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
isBound = true;
Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
startService(intent);
}
@Override
public void onServiceDisconnected(ComponentName className)
{
Toast.makeText(MainActivity.this, "Service Dis-connected", Toast.LENGTH_LONG).show();
m_service = null;
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}
.
.
.
}
回答by Bharat
You need to add this in your code.
您需要在代码中添加它。
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(m_serviceConnection);
Toast.makeText(MainActivity.this, "Service Un-Binded", Toast.LENGTH_LONG).show();
};
回答by Teocci
Another good practice to do this could be adding a flag to know when the service was added.
另一个很好的做法是添加一个标志来了解何时添加服务。
private boolean m_serviceBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
...
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
m_serviceBound = true;
...
}
@Override
protected void onDestroy() {
...
if (m_serviceBound) {
unbindService(m_serviceConnection);
m_serviceBound = false;
}
...
};
This is useful if you bind and unbind the service in different segments of the app's life cycle.
如果您在应用程序生命周期的不同阶段绑定和取消绑定服务,这将非常有用。
回答by Rohit Singh
onStop()
停止()
is another place where you can unbind a service in your activity.
是另一个您可以在您的活动中取消绑定服务的地方。
This approach is quite preferable when you want to interact with a service only when your Activity is in the foreground. See official Android Developer guide suggestion
当您只想在您的 Activity 在前台时与服务交互时,这种方法非常可取。查看官方 Android 开发者指南建议
Override
protected void onStop() {
super.onStop();
unbindService(m_serviceConnection);
};
In this case, prefered way to bind service is in the onStart()
method of the Activity.
在这种情况下,绑定服务的首选方式是在onStart()
Activity的方法中。