简体   繁体   English

如何启动自动停止的android服务?

[英]How to start the automatically stopped android service?

I have written a service, it runs at start up, but suddenly due to some run time errors the service is stopping. 我已经编写了一个服务,该服务在启动时运行,但是由于某些运行时错误,该服务突然停止了。 How can I re run the automatically stopped service (because of run time exception). 如何重新运行自动停止的服务(由于运行时异常)。

there is a 有一个

stopSelf()
stopSelf(startId)

method in service which you can use to stop a service from inside. 服务中使用的方法,可用于从内部停止服务。

EDIT 编辑

i guess i need some rest. 我想我需要休息。

well if you want to find if a service is stopping due to some runtimeexecption 很好,如果您想查找服务是否由于某些运行时执行而停止

can you try 你能试一下吗

    private Thread.UncaughtExceptionHandler onRuntimeError= new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            //Try starting the service again
                    //May be a pending intent might work
    };

in your on create 在您创建时

    @Override
    protected void onCreate() { 
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);  
    }

Maybe you could get a second service that monitors the first service, and restarts it if its stops responding. 也许您可以获得监视第二项服务的第二项服务,并在其停止响应时重新启动它。 I think that is basically what the watchdog in Android does for the system-services. 我认为这基本上就是Android中的看门狗对系统服务所做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM