I use AlarmManager
to start service every 1 minute to perform some background job.
Here is code snippet:
Intent intent = new Intent(context, PostLocationService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, POST_LOCATION_REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + TEN_SECONDS_INTERVAL,
ONE_MINUTE_INTERVAL, pendingIntent);
PostLocationService
extends Service
. In onStartCommand()
I do some job and call stopSelf()
after this job is done.
But I know that if this service is still running, next time on receiving intent onStartCommand()
will be executed again and needed job will be done.
What is the best practice here: start new service every 1 minute or keep one service running and waiting for intents?