Unreliable ScheduledExecutorService in Service

自古美人都是妖i 提交于 2020-03-25 18:46:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!