如何启动自动停止Android的服务? [英] How to start the automatically stopped android service?

查看:129
本文介绍了如何启动自动停止Android的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个服务,它运行在启动时,却突然因为一些服务正在停止运行时错误。我怎样才能重新运行自动停止服务(因为运行时异常)。

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).

推荐答案

有一个

stopSelf()
stopSelf(startId)

方法的服务,您可以使用从内部停止服务。

method in service which you can use to stop a service from inside.

修改

我想我需要一些休息。

好,如果你想找到服务是否有些runtimeexecption由于停止

well if you want to find if a service is stopping due to some runtimeexecption

您可以尝试

    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
    };

在上创建

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

这篇关于如何启动自动停止Android的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆