-1

I need to write an app which will be able to run in background even if it was destroyed. Also it must call activities in some random periods of time. activity should contain edittext, imageview and button. I thought that it is possible to do using service but the service stops when i destroy the app.

3 Answers3

0

you can register scheduled task to alarmmanager which launches your broadcast every few minutes and in broadcast you can check if your service is alive or not if it wasn't alive you can launch your service and you should extend your service from IntentService to avoid Blocking Ui Thread

and if you want to run your service after device turning on you can use broadcast which listens to device_boot_complete event and then you can register your alarmmanager task

Alarmmanager example

Device_Boot_Completeed example

Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
0

I thought that it is possible to do using service but the service stops when i destroy the app.

No, if you start your service with START_STICKY then android will keep your service live even if app is destroyed or even user force stop app. You can start forever thread by handler in your service.

For more info about START_STICKY see this link

Let me know if this clears your confusion.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
-1

You can send broadcast from onTaskRemoved method of your service as follows:

Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);

and have an broadcast receiver which will again start a service

You can read more at: https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android

hientp
  • 634
  • 4
  • 11