我有一个运行为web服务器的应用程序。该应用程序具有START_STICKY服务,我希望该服务一直运行Web服务器(在通知中向用户提供停止选项)。
问题在于当我滑动关闭应用程序时,服务器将被重新启动(丢失设置等)。它可以正常运行,但logcat显示它正在重新启动。
我可以重新打开我的应用程序并绑定到新服务,这很好。尽管再次滑动关闭会产生相同的影响。
我需要使其不重新启动。
标准服务代码
问题在于当我滑动关闭应用程序时,服务器将被重新启动(丢失设置等)。它可以正常运行,但logcat显示它正在重新启动。
我可以重新打开我的应用程序并绑定到新服务,这很好。尽管再次滑动关闭会产生相同的影响。
我需要使其不重新启动。
标准服务代码
private WebServerService mService;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder binder) {
WebServerService.MyBinder b = (WebServerService.MyBinder) binder;
mService = b.getService();
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
public serviceStart() {
mIntent = new Intent(mContext.getApplicationContext(), WebServerService.class);
mContext.startService(mIntent);
mContext.bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE);
}
服务启动
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, START_STICKY, startId);
Log.d("SERVICE","Started");
return START_STICKY;
}