Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 崩溃的服务在很长一段时间后重新启动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在前台运行服务.有时它会被终止,大概是出于内存的原因(尽管我对LogCat日志并不是100%肯定).已终止的其他服务计划在5000毫秒内重新启动,但查看日志,我的服务重新启动时间很长,例如:
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(未曝光)
// How long we wait for a service to finish execuTing.
static final int serviCE_TIMEOUT = 20*1000;

// How long a service needs to be running until restarTing its process
// is no longer considered to be a relaunch of the service.
static final int serviCE_RESTART_DURATION = 5*1000;

// How long a service needs to be running until it will start BACk at
// serviCE_RESTART_DURATION after being killed.
static final int serviCE_RESET_RUN_DURATION = 60*1000;

// Multiplying factor to increase restart duration time by,for each time
// a service is killed before it has run for serviCE_RESET_RUN_DURATION.
static final int serviCE_RESTART_DURATION_FACTOR = 4;

// The minimum amount of time @R_262_7000@n restarTing services that we allow.
// That is,when multiple services are restarTing,we won't allow each
// to restart less than this amount of time from the last one.
static final int serviCE_MIN_RESTART_TIME_@R_262_7000@N = 10*1000;

// Maximum amount of time for there to be no activity on a service before
// we consider it non-essential and allow its process to go on the
// LRU BACkground list.
static final int MAX_serviCE_INACTIVITY = 30*60*1000;

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

从第881行开始:

// If it has been a "reasonably long time" since the service
            // was started,then reset our restart duration BACk to
            // the beginning,so we don't infinitely increase the duration
            // on a service that just occasionally gets killed (which is
            // a normal case,due to process being killed to reclaim memory).
            if (Now > (r.restartTime+resetTimE)) {
                r.restartCount = 1;
                r.restartDelay = minDuration;
            } else {
                if ((r.serviceInfo.applicationInfo.flags
                        &ApplicationInfo.FLAG_PERSISTENT) != 0) {
                    // services in peristent processes will restart much more
                    // quickly,since they are pretty important.  (Think systemUI).
                    r.restartDelay += minDuration/2;
                } else {
                    r.restartDelay *= serviCE_RESTART_DURATION_FACTOR;
                    if (r.restartDelay < minDuration) {
                        r.restartDelay = minDuration;
                    }
                }
            }

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

r.restartDelay *= serviCE_RESTART_DURATION_FACTOR;

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

大佬总结

以上是大佬教程为你收集整理的android – 崩溃的服务在很长一段时间后重新启动全部内容,希望文章能够帮你解决android – 崩溃的服务在很长一段时间后重新启动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。