Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why so slow Android service restarting with START_STICKY?

I have a background service, and I am doing all the operations on this service.
The service is working with activities at times. But if the application closes, the service restart with START_STICKY; It works correctly, but sometimes it takes a long time to restart, like more than a minute.

@Override
public void onCreate() {
    SocketIOConnect();
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {      
    return START_STICKY;
}

How do I reduce the restart time?

like image 797
erginduran Avatar asked Aug 29 '14 13:08

erginduran


People also ask

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. Here you will lose the results that might have computed before. START_NOT_STICKY- tells the system not to bother to restart the service, even when it has sufficient memory.

What should onStartCommand () return incase if the service gets killed by OS?

Constant to return from onStartCommand(Intent, int, int) : if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int) ), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int) .

What is sticky service in Android?

Sticky – This tells Android to restart the Service, but not to deliver the last Intent that started the Service. If there are no pending Intents to handle, then a null will be provided for the Intent parameter.

Does service run on main thread Android?

Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. You should run any blocking operations on a separate thread within the service to avoid Application Not Responding (ANR) errors.


2 Answers

How do I reduce restart time?

You do not control this. It is up to the OS to determine when it will restart services whose processes were terminated for one reason or another.

Bear in mind that your service might never restart, if the user force-stopped your app (e.g., from Settings).

like image 79
CommonsWare Avatar answered Sep 30 '22 21:09

CommonsWare


You cannot control that. But however, I could suggest some good practices.

  • You should call the super.onCreate(); first before calling other methods inside your overrided method.
  • If your service class is extended from IntentService class, and if you decide to also override other callback methods, such as onCreate(), onStartCommand(), or onDestroy(), be sure to call the super implementation, so that the IntentService can properly handle the life of the worker thread. check from android developer side

You can call the super.onStartCommand() as follows:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, START_STICKY, startId);
    return START_STICKY;
}
  • The constant variable START_STICKY will let the service run indefinitely until you explicitly call stopService() or stopSelf(). Therefore, make sure that you have the option to stop it, otherwise you have to force stop it from Application Settings.
  • You can add a category TAG to your service before starting so then you can use that TAG to uniquely identify and stop the service externally by calling stopService() method.

Starting service

serviceIntent = new Intent(MainActivity.this, MyService.class);
                serviceIntent.addCategory(MYTAG);
                startService(serviceIntent);

Stopping service

serviceIntent = new Intent(MainActivity.this, MyService.class);
                    serviceIntent.addCategory(MYTAG);
                    stopService(serviceIntent);
like image 41
Vishnuvathsan Avatar answered Sep 30 '22 23:09

Vishnuvathsan