9

I have an app that runs as a web server. The app has a service that is START_STICKY I want this service to run the web server all the time (option is given to user in a notification to stop it).

Problem is that the server is restarted (loosing settings etc) when i swipe my app closed. It stays there fine but logcat shows that it is restarting.

I can re- open my app and bind to the new service, this works fine. Although swipe closing again has the same effect.

I need this to NOT restart.

Standard service code

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

Service on start

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, START_STICKY, startId);
    Log.d("SERVICE","Started");
    return START_STICKY;
}
4
  • Take a look at this answer by @fedepaol [Android Background Service is restarting when application is killed][1] [1]: stackoverflow.com/questions/15452935/…
    – Maclaren
    Commented Aug 5, 2015 at 19:55
  • this is not what I am after, it talks about not wanting it to restart at all! I want it not ti even close. Thanks though, I think it is something after I unbind.
    – RuAware
    Commented Aug 5, 2015 at 20:17
  • Your service can and will be killed by the system, there is nothing you can do about it. But, if you have start sticky you will be started again with a null intent. You'll have to restore your state after you get restarted. Commented Aug 6, 2015 at 5:35
  • Can you provide the code that start the service please? Constant START_STICKY means service should restart after being closed so.if you don't want it to restart after being closed then START_NOT_STICKY will be appropriate Commented Aug 9, 2015 at 20:10

4 Answers 4

10
+50

Short answer: you can't. Every Android app can be killed by the system or by the user when the system claims memory or the user swipes out the app from the recent apps list. This is an Android design and all apps must adhere to it. The only (small) improvement you can have is setting the service as a Foreground service:

where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

2
  • 1
    Thank you for your help. The startForeground() does stop the service from restarting when user swipe closes the app. Which is what I was after. I understand that if the memory is low it will restart, which I handle. But this so far has not happened in any of my tests.
    – RuAware
    Commented Aug 12, 2015 at 19:58
  • you saved my day
    – yarin
    Commented Sep 9, 2018 at 21:22
2

Use startForeground()

The drawback is that you'll have to provide a notification.

1

Like Mimmo said: You can't. The system can indeed kill apps/services if low on memory. Also users can do this. Either with the force close button in app settings or swiping the app. Swipe to close is like the force stop. The app/service DOES NOT RESTART when closing an app like this. That is just how the system works. Try it yourself, download advance task killer and kill Facebook for example. If you restart advance task killer a few seconds later, you will see Facebook is running again. Now open Facebook and use the swipe function to kill it. Now start the task killer again. You will see Facebook is not running anymore. Again, that is how the system works. Setting the service as a Foreground service is not going to help either.

0

Will the tag android:process="anyname" on your manifest be useful? It makes the service run on a different process of your app.

http://developer.android.com/guide/topics/manifest/service-element.html#proc

As appointed by others it still can be terminated by the system if running low on memory but it won't terminate by closing your app. Hope it helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.