Home » Android » android – Service restarted on Application Close – START_STICKY

android – Service restarted on Application Close – START_STICKY

Posted by: admin June 15, 2020 Leave a comment

Questions:

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;
}
How to&Answers:

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.)

Answer:

Use startForeground()

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

Answer:

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.

Answer:

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.