1

I'm running an Android Service that is started after boot completed. However, when I shutdown my phone, the Service (and all to it connected Threads) don't seem to be destroyed.

I have a Socket connection running in the background and it is not disconnected on shutdown. Moreover, not even service.onDestroy() is called.

Is that normal or might it be due to another problem of mine?

FreddyMro
  • 76
  • 5
  • It does seem like it should be, although if the phone runs out of batteries it might not get a chance. See https://stackoverflow.com/questions/10448304/handling-phone-shutdown-event-in-android – Kevin Krumwiede May 26 '17 at 22:45
  • 1
    Please explain **precisely** what you mean by "shutdown". Normally, I would interpret that to mean "powered off", in which case your socket connection most certainly is no longer running, as the CPU, RAM, WiFi, cellular radio, and everything else will have no power. – CommonsWare May 26 '17 at 22:48
  • Just like Activity.onDestroy(), there is no guarantee that Service.onDestroy() will be called because the whole app process may be killed. That's probably what happens when you shut down the phone, all app processes are simply killed. – BladeCoder May 26 '17 at 22:50
  • @CommonsWare By shutdown I mean Power Off. I will try to find another way to solve it. – FreddyMro May 27 '17 at 23:17

2 Answers2

0

you can use Reciever which can access the other mobile components status such as internet connectivity status, GPS status and as soon as mobile boots it will start your Service for simple example.

0

check the flag which you set as a return from onStartCommand :

START_STICKY

the system will try to re-create your service after it is killed

START_NOT_STICKY

the system will not try to re-create your service after it is killed