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.
-
Read at least something about Android development. – Ghulam Moinul Quadir Apr 12 '18 at 15:27
-
What exactly is your question? – abdul Apr 12 '18 at 15:42
-
@GhulamMoinulQuadir what a helpful answer. thank you – mark vincha Apr 12 '18 at 15:52
3 Answers
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

- 2,325
- 1
- 9
- 17
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.

- 57,232
- 27
- 203
- 212
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

- 634
- 4
- 11