0

I realized an android application which always has to remain activated in the background. To make that, it is enough to use a Service which remains active after the user is left the application.

Nevertheless, if the user rebooting, the application as well as the Service will not be any more active.

How to solve this problem?

I think of having an idea with BroadcastReceiver but I am not on whether it is the best solution.

Thanks in Advance.

Community
  • 1
  • 1
lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110

2 Answers2

2

So you want the service to restart if the phone is rebooted ?

Create a BroadcastReceiver that listens for BOOT_COMPLETE (you need a permission for this). Have it start your service.

Android may stop your service due to lack of memory.If you have onStartCommand return START_STICKY and have your onCreate of the service call startService on itself, you'll restart when memory is available.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Detailed description with examples, found it really useful [https://github.com/codepath/android_guides/wiki/Starting-Background-Services](https://github.com/codepath/android_guides/wiki/Starting-Background-Services) – Ivar Sep 03 '15 at 00:59
0

You can set up an alarm to wake your service if its not running, and set up a broadcast receiver on boot which will start the service when the device boot.

Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67