Android 执行器的可靠性。newSingleThreadScheduledExecutor()。计划
这个遗嘱执行人有什么已知的问题吗?还是我用错了?我需要在单独的线程中安排上传,并且我希望在当前上传完成后的一段时间后启动下一次上传 下面是我的服务实现的一些代码摘录:Android 执行器的可靠性。newSingleThreadScheduledExecutor()。计划,android,multithreading,scheduledexecutorservice,Android,Multithreading,Scheduledexecutorservice,这个遗嘱执行人有什么已知的问题吗?还是我用错了?我需要在单独的线程中安排上传,并且我希望在当前上传完成后的一段时间后启动下一次上传 下面是我的服务实现的一些代码摘录: ScheduledExecutorService periodicUploadExecutor; @Override public void onCreate() { super.onCreate(); // some stuff... periodicUploadExecutor = Executo
ScheduledExecutorService periodicUploadExecutor;
@Override
public void onCreate() {
super.onCreate();
// some stuff...
periodicUploadExecutor = Executors.newSingleThreadScheduledExecutor();
periodicUploadExecutor.schedule(uploadPointsToServer, getCurrentUploadIntervalInMs(), TimeUnit.MILLISECONDS);
}
private Runnable uploadPointsToServer = new Runnable() {
public void run() {
Log.d("SOMETAG", "--->>> UPLOAD runnable started!");
// upload stuff here...
Log.d("SOMETAG", " upload runnable scheduling next after "+getCurrentUploadIntervalInMs());
periodicUploadExecutor.schedule(uploadPointsToServer, getCurrentUploadIntervalInMs(), TimeUnit.MILLISECONDS);
Log.d("SOMETAG", "<<<--- upload runnable ENDED!");
}
}
private final int getCurrentActiveSampIntervalInMs() {
return 300000; // just an example
}
ScheduledExecutorService periodicUploadExecutor;
@凌驾
public void onCreate(){
super.onCreate();
//一些东西。。。
periodicUploadExecutor=Executors.newSingleThreadScheduledExecutor();
periodicUploadExecutor.schedule(uploadPointsToServer,getCurrentUploadIntervalInMs(),时间单位为毫秒);
}
private Runnable uploadPointsToServer=new Runnable(){
公开募捐{
Log.d(“SOMETAG”,“已启动-->>>上载可运行!”);
//上传资料到这里。。。
Log.d(“SOMETAG”,“upload runnable scheduling next after”+getCurrentUploadIntervalInMs());
periodicUploadExecutor.schedule(uploadPointsToServer,getCurrentUploadIntervalInMs(),时间单位为毫秒);
Log.d(“SOMETAG”,”您需要使用AlarmManager
而不是Executor/handler,或者使用Partial来保持cpu的运行
如果cpu处于睡眠模式,则在手机唤醒之前,您的应用程序不会运行。
AFAIK onlyAlarmManager
可以通过唤醒设备获得回调。为此,您需要使用Expressed\u REALTIME\u WAKEUP
或RTC\u WAKEUP
类型,其他选项将再次导致延迟。如果您正在安排,那么您应该使用AlarmManager。通常服务可能会在mi中停止ddle,如果Cpu处于睡眠模式,调度程序将不会唤醒Cpu,但alarmManager将使我惊讶的是,第二个runnable在启动之前不会等待3分钟starting@fiddler3000000毫秒是5分钟,正如您在日志中看到的,前三个的每一个都在前一个5分钟后启动finishes@nandeesh谢谢你的评论,我接受但在这种特殊情况下,服务并没有停止,主线程中正在进行位置管理器更新(为了清楚起见,我从日志中删除了它们)在此期间手机显示屏是否关闭。如果手机显示屏关闭,则主要意味着它处于睡眠状态。在此期间,无法安排任何作业。如果需要在手机未进入睡眠模式的情况下安排作业,则需要唤醒锁
01-08 15:33:42.166 D/SOMETAG ( 4606): --->>> UPLOAD runnable started!
01-08 15:33:43.166 D/SOMETAG ( 4606): upload runnable scheduling next after 300000
01-08 15:33:43.166 D/SOMETAG ( 4606): <<<--- upload runnable ENDED!
01-08 15:38:43.166 D/SOMETAG ( 4606): --->>> UPLOAD runnable started!
01-08 15:38:44.174 D/SOMETAG ( 4606): upload runnable scheduling next after 300000
01-08 15:38:44.174 D/SOMETAG ( 4606): <<<--- upload runnable ENDED!
01-08 15:43:44.174 D/SOMETAG ( 4606): --->>> UPLOAD runnable started!
01-08 15:43:45.143 D/SOMETAG ( 4606): upload runnable scheduling next after 300000
01-08 15:43:45.143 D/SOMETAG ( 4606): <<<--- upload runnable ENDED!
01-08 16:01:38.887 D/SOMETAG ( 4606): --->>> UPLOAD runnable started!