I have an Android service that starts a timer that does stuff:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer.scheduleAtFixedRate(new VeryImportantTask(), 0, RATE);
return START_STICKY;
}
I know Services are singleton but, does onStartCommand
method called each time that I call startService()
? If so, I should control that my timer is just started the first time, shouldn't I? I'm thinking in a static boolean flag in the service. Is there a better way?
In your case i.e START_STICKY you can simply check the intent value
nullity
like thisbecause first time the intent will not be null and it will be null every time in case of a restart with
START_STICKY
.