Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service restarted on Application Close - START_STICKY

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;
}
like image 913
RuAware Avatar asked Aug 01 '15 08:08

RuAware


People also ask

What is start_ STICKY in Android?

START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent. START_NOT_STICKY tells the OS to not bother recreating the service again.

What is start_ STICKY?

START_STICKY- tells the system to create a fresh copy of the service, when sufficient memory is available, after it recovers from low memory.

What is startForeground in Android?

A started service can use the startForeground(int, android. app. Notification) API to put the service in a foreground state, 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.

What is a service in Android?

A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application.


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

like image 182
Mimmo Grottoli Avatar answered Sep 30 '22 17:09

Mimmo Grottoli