Sticky service not restarting when created in custom object

江枫思渺然 提交于 2019-12-02 05:21:55

问题


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);
    }
}

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                                                                                                                                                              
    }
}

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.

Is starting service from activity different from starting it from a regular java object?


回答1:


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;
}

this will restart the service in 5-10 seconds after app killed.



来源:https://stackoverflow.com/questions/39344155/sticky-service-not-restarting-when-created-in-custom-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!