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?
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.
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) .
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.
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.
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).
You cannot control that. But however, I could suggest some good practices.
super.onCreate();
first before calling other methods inside your overrided
method.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;
}
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.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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With