我正在尝试在Android中使用服务进行一些基本的数据库操作,但由于某种原因我得到一个Activity已泄露ServiceConnection错误.我将在底部发布完整的Logcat读数.
我必须在多个活动中使用相同的服务,所以我创建了一个Superclass来处理所有服务任务.它看起来像这样:
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) …Run Code Online (Sandbox Code Playgroud)