问题
I created a ScheduledExecutorService
in my Service
that shuold run a runnable regulary. During debuging all it´s working fine. But on the smartphone the runnable
is executed much later and the Service
crashes occasionaly.
The runnable is scheduled every 8-30 minutes. What can I do to make the Service
more reliable? Are there better ways to realize this plan? (before I was using Thread
and ExecutorService
with nearly the same results). Here my current code:
public class MyService extends Service {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
//do something regulary
scheduler.schedule(this,getDelay(),MILLISECONDS);
}
};
//create notification channel, notification and startForegorund()
scheduler.schedule(runnable,1,MINUTES);
return START_STICKY;
}
来源:https://stackoverflow.com/questions/60581400/unreliable-scheduledexecutorservice-in-service