android – 崩溃的服务在很长一段时间后重新启动

前端之家收集整理的这篇文章主要介绍了android – 崩溃的服务在很长一段时间后重新启动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在前台运行服务.有时它会被终止,大概是出于内存的原因(尽管我对LogCat日志并不是100%肯定).已终止的其他服务计划在5000毫秒内重新启动,但查看日志,我的服务重新启动时间很长,例如:
  1. 11-15 15:39:48.756: W/ActivityManager(375): Scheduling restart of crashed service com.example.app/com.example.SensorService in 1019562ms

有什么可以解释这个?如何设置重启间隔?我已经看到一些建议,通知服务重新启动时延迟较短,但我的服务确实有通知.

解决方法

com/android/server/am/ActiveServices.java(未曝光)
  1. // How long we wait for a service to finish executing.
  2. static final int SERVICE_TIMEOUT = 20*1000;
  3.  
  4. // How long a service needs to be running until restarting its process
  5. // is no longer considered to be a relaunch of the service.
  6. static final int SERVICE_RESTART_DURATION = 5*1000;
  7.  
  8. // How long a service needs to be running until it will start back at
  9. // SERVICE_RESTART_DURATION after being killed.
  10. static final int SERVICE_RESET_RUN_DURATION = 60*1000;
  11.  
  12. // Multiplying factor to increase restart duration time by,for each time
  13. // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
  14. static final int SERVICE_RESTART_DURATION_FACTOR = 4;
  15.  
  16. // The minimum amount of time between restarting services that we allow.
  17. // That is,when multiple services are restarting,we won't allow each
  18. // to restart less than this amount of time from the last one.
  19. static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
  20.  
  21. // Maximum amount of time for there to be no activity on a service before
  22. // we consider it non-essential and allow its process to go on the
  23. // LRU background list.
  24. static final int MAX_SERVICE_INACTIVITY = 30*60*1000;

你发生的事情可能是你的服务比SERVICE_RESET_RUN_DURATION快死,然后重启时间乘以SERVICE_RESTART_DURATION_FACTOR.

从第881行开始:

  1. // If it has been a "reasonably long time" since the service
  2. // was started,then reset our restart duration back to
  3. // the beginning,so we don't infinitely increase the duration
  4. // on a service that just occasionally gets killed (which is
  5. // a normal case,due to process being killed to reclaim memory).
  6. if (now > (r.restartTime+resetTime)) {
  7. r.restartCount = 1;
  8. r.restartDelay = minDuration;
  9. } else {
  10. if ((r.serviceInfo.applicationInfo.flags
  11. &ApplicationInfo.FLAG_PERSISTENT) != 0) {
  12. // Services in peristent processes will restart much more
  13. // quickly,since they are pretty important. (Think systemUI).
  14. r.restartDelay += minDuration/2;
  15. } else {
  16. r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;
  17. if (r.restartDelay < minDuration) {
  18. r.restartDelay = minDuration;
  19. }
  20. }
  21. }

下面的行将支持您的服务.

  1. r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;

因此,如果您的服务在运行SERVICE_RESET_RUN_DURATION之前死亡,您应该修复此案例.

猜你在找的Android相关文章