[Q] Restart service after kill

Search This thread

Rick Clephas

Senior Member
Jul 17, 2014
763
528
Hi I want to start my service after it is killed by the user or the system.
I thought about creating a second app and monitor if the service is running and if not then start it.
The problem I have with that is
- I need two apps
- It drains more battery

I hope you can help me find the best solution for my problem :)
 

hiphop12ism

Senior Member
Aug 24, 2012
58
2
Yeah, I think if you override the onDestroy method on your service class, and then you can send a broadcast from there.
In your broadcast receiver you can start the service again.

Though i have never tried it myself, i think this should work.
Tell me how it goes


Happy coding!! :thumbup:

Sent from my GT-P3100 using XDA Free mobile app
 
  • Like
Reactions: Rick Clephas

Rick Clephas

Senior Member
Jul 17, 2014
763
528
Yeah, I think if you override the onDestroy method on your service class, and then you can send a broadcast from there.
In your broadcast receiver you can start the service again.

Though i have never tried it myself, i think this should work.
Tell me how it goes


Happy coding!! :thumbup:

Sent from my GT-P3100 using XDA Free mobile app

I thought about that too but unfortunately when the app is stopped by the user the onDestroy methode is not called...

Sent from my SM-N9005 using XDA Premium 4 mobile app
 

hiphop12ism

Senior Member
Aug 24, 2012
58
2
If yu are using android version less than 4.4.2 then returning start sticky should do it

Sent from my GT-P3100 using XDA Free mobile app
 
Last edited:

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
That is only if the system stops the service not if the user does....

I believe there is no such way... otherwise android would be very insecure for malware that just does this ;). You can only restart your service once the user restarts your activity, there should not be another way (and START_STICKY for the system of course). And please don't do two apps which listen for each other, if the user willingly kills your service they usually have a reason and expect that your app is not working right until they restart it.
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
I believe there is no such way... otherwise android would be very insecure for malware that just does this ;). You can only restart your service once the user restarts your activity, there should not be another way (and START_STICKY for the system of course). And please don't do two apps which listen for each other, if the user willingly kills your service they usually have a reason and expect that your app is not working right until they restart it.

I have heard that a lot but does that mean that all the security apps available on the play store can be killed by the user? :what:

Sent from my SM-N9005 using XDA Premium 4 mobile app
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
Well I found the solution.
When device admin is enabled the app can't be force-stopped or uninstalled. Then the user can only stop the service and then the onDestroy methode is called so then I can restart the service again. Tanks guys :thumbup:

Sent from my SM-N9005 using XDA Premium 4 mobile app
 

Top Liked Posts