Full Source Code: https://github.com/varunon9/DynamicWallpaper/tree/always_running_service
- Create a Foreground Service (MyService.java)
- Create a Manifest registered Broadcast Receiver (MyReceiver.java) which will start your Foreground Service
- In
onDestroy
lifecycle of MyService, send a broadcast intent to MyReceiver - Launch the MyService on app start from MainActivity (see step 8)
- With above 4 steps, MyService will always get re-started when killed as long as
onDestroy
of Service gets called onDestroy
method of Service is not always guaranteed to be called and hence it might not get started again