Reputation: 17654
I want to restart my android service if some preferences have changed, but there's nothing like a restart method in the Service class? Is there any way to restart my service, except creating some kind of reset method that resets all class variables etc. ? Thanks for any hint!
Upvotes: 1
Views: 473
Reputation: 635
You have to override
the onStartCommand()
in your service class 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
indicates the system to restart the service if it get killed.
You should have/implement a listener
to detect and inform when preference changes and you should register for that listener inside onStart()
method and un-register that listener inside onDestroy()
.
Upvotes: 1
Reputation: 1703
I think that calling it again, will call onStartCommand() again. This might do the work for you.
*it will run onCreate() if it's not alive.
Call startService(intent) again, that will do the trick
Upvotes: 1