0

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

Girish
  • 123
  • 1
  • 4
  • 14

2 Answers2

1

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);  
    }
Samuel
  • 9,883
  • 5
  • 45
  • 57
0

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.

dac2009
  • 3,521
  • 1
  • 22
  • 22