在自定义对象中创建粘性服务后不会重新启动 [英] Sticky service not restarting when created in custom object
问题描述
我有一个具有绑定服务的Singleton对象.我希望它重新启动,并且当我从启动器启动应用程序时,单例对象将初始化并绑定到该服务的现有实例.
I had a Singleton object that had a bound service. I wanted it to restart, and when I start my application from launcher, singleton object will initialize and bind to this existing instance of service.
以下是用于创建和绑定服务的代码:
Here is the code for creating and binding service in singleton:
public class MyState {
private static MyState sState;
private MyService mService;
private Context mContext;
private boolean mBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.MyBinder binder = (MyService.MyBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
public static MyState get(Context context) {
if (sState == null) {
sState = new MyState(context);
}
return sState;
}
public MyState(Context context) {
mContext = context.getApplicationContext();
startService();
}
private void startService() {
Intent intent = new Intent(mContext, MyService.class);
mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
// this won't create another instance of service, but will call onStartCommand
mContext.startService(intent);
}
}
这是代码insice服务
And here is the code insice Service
public class MyService extends Service {
private final IBinder mBinder = new MyBinder();
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// this method is called by singleton object to stop service manually by user
public void stop() {
stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
// some cleanup code here
}
}
不幸的是,当我在任务列表中轻扫我的应用程序时,该服务永远不会重新启动.在这种情况下,永远不会调用Service的onDestroy方法.
Unfortunately, when I swipe away my app in task list this service never restarts. Service's onDestroy method is never called in this case.
我将绑定移动到一个用户可以与服务进行交互的活动,并且令人惊讶的是,绑定开始按我期望的那样工作. 我试图在活动中使用应用程序上下文来调用服务创建,但该方法仍然有效.
I moved the binding to an activity at which user can interact with service, and surprisingly it started working as I expected. I tried to call service creation using application context inside my activity, and it still works.
从活动启动服务是否不同于从常规Java对象启动服务?
Is starting service from activity different from starting it from a regular java object?
推荐答案
当您返回START_STICKY
时,此服务将在您关闭/杀死该应用程序时停止,因为在该应用程序关闭后,所有引用/值都将变为null
所有Intent以及变量,因此STICKY
服务将无法获得Intent值.如果您想在应用终止后重新启动服务,请使用return START_REDELIVER_INTENT
As you are returning START_STICKY
this service will stop whenever you close/kill the app because after the App closed all the Reference/Value will become null
for all Intent as well as variables and so STICKY
service will not able to get Intent value. if you want to restart the service after app kills use return START_REDELIVER_INTENT
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
}
这将在应用终止后的5-10秒内重新启动服务.
this will restart the service in 5-10 seconds after app killed.
这篇关于在自定义对象中创建粘性服务后不会重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!