你好,我有一个小的聊天应用程序,它绑定(bind)到我的服务,该服务创建与我的服务器的 tcp/ip 连接。 顺便说一句,我使用 aidl。
我的问题是我不知道如何处理屏幕方向,尤其是在 destroy 方法上。
我希望我的服务一直在线,我不想在应用程序销毁时停止它。
现在我使用“onDestroy”方法 unbindService(conn) 来解决“连接泄漏问题”。
Activity 被销毁后重新创建时它会卡住。 谢谢,这里是一些代码 fragment 。
serviceIntent = new Intent(getApplicationContext(),ChatService.class);
boolean result = bindService(serviceIntent, conn,Context.BIND_AUTO_CREATE);
startService(new Intent(getApplicationContext(),ChatService.class));
Log.d(TAG, "Service binded");
@Override
protected void onDestroy() {
Log.d(TAG, "ON DESTROY");
unbindService(conn);
super.onDestroy();
}
最佳答案
服务没有用户界面,因此你可能会说这不取决于你的服务,而是你的 Activity 。在 onStartCommand()
中返回 START_STICKY
告诉你即使你解除绑定(bind)它也必须运行,基本上你可以随时绑定(bind)/解除绑定(bind)到持久(非持久)直到 android 操作系统决定终止它)运行服务。
在您的 Activity 中覆盖 onConfigurationChange()
方法并且不执行任何操作,然后在您的 list 中使用 android:configChanges="orientation"
。
关于Android 解绑服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11935625/