绑定服务和超时
在我正在开发的应用程序中,有一个绑定服务,它使用 TCP 套接字连接。服务的要求是在绑定到服务的最后一个 Activity 关闭后保持套接字连接一段时间,例如 1 分钟。这是为了避免当另一个活动在一个活动与该服务解除绑定后立即连接到该服务时,不必要的套接字重新连接。
我搜索并发现了类似的问题(服务,重新绑定还是根本不绑定?< /a> )建议使用已启动的服务,但当前的应用程序架构使用绑定连接,如果不需要,我不想重新设计。因此我正在寻找其他选择。
我的问题是,我能否以某种方式推迟绑定服务的销毁,或者唯一好的方法是使用意图重写与服务的通信,从而将其转换为已启动的服务?
public class SocketService extends Service {
private static final String LOG_TAG = SocketService.class.getSimpleName();
@Override
public final IBinder onBind(final Intent intent) {
Log.d(LOG_TAG, "onBind()");
return new LocalBinder<SocketService>(this);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(LOG_TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
Log.d(LOG_TAG, "onCreate");
// create socket connection here
// ...
//
super.onCreate();
}
@Override
public void onDestroy() {
Log.d(LOG_TAG, "onDestroy");
// close socket connection
// ...
//
super.onDestroy();
}
// Other socket sending and receiving logic
// ...
//
}
提前感谢您抽出时间。
In the application that I am developing there is a bound service, that uses TCP socket connection. The requirement for the service is to keep the socket connection for a while after last Activity bound to the service is shut down, say for 1 minute. This is to avoid unneeded socket reconnections when another activity connects to the service just after one has unbound from it.
I have searched and found similar issue ( Service, Rebind or not bound at all? ) suggesting using started service, but current application architecture uses bound connections, and I would not like to redesign if it is not needed. Hence I am looking for other options.
My question is, can I somehow postpone bound service destruction or the only good approach is to rewrite the communication to the service using intents, thus converting it to started service?
public class SocketService extends Service {
private static final String LOG_TAG = SocketService.class.getSimpleName();
@Override
public final IBinder onBind(final Intent intent) {
Log.d(LOG_TAG, "onBind()");
return new LocalBinder<SocketService>(this);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(LOG_TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
Log.d(LOG_TAG, "onCreate");
// create socket connection here
// ...
//
super.onCreate();
}
@Override
public void onDestroy() {
Log.d(LOG_TAG, "onDestroy");
// close socket connection
// ...
//
super.onDestroy();
}
// Other socket sending and receiving logic
// ...
//
}
Thanks in advance for you time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持您启动的第一个 Activity 与服务绑定,当您启动另一个 Activity 时不要破坏它。
当您想要从当前活动退出应用程序时,请向服务发送一个信号,该服务将通知所有活动关闭。
OnCreate 仅在第一次绑定到服务时调用(当服务不存在时),随后的绑定不应触发 onCreate 函数。
Keep the First Activity you launch bound to the service , don't destroy it when you launch another one .
When you want to exit the application from your current activity send a signal to the service which will notify all activities to close .
OnCreate is only called on the first bind to the service (when the service doesn't exist ) , the bind that follows should not trigger the onCreate function .