I make an application that will be from the GUI and which should also constantly work in the background even if the GUI itself is closed / destroyed / destroyed. It should start automatically when the phone boots.

In the background, it will send data to the server, say, every 10 seconds.

Googling, I concluded that one of the ways to do this is Service. And the second is the ScheduledThreadPoolExecutor. The rest are not suitable (alarm manager, timer, handler).

What do you advise? Which of these methods to choose? Which is better for my task?

  • one
    Why is the AlarmManager not suitable? - Android Android
  • @AndroydAndroyd, I don’t know, but it seemed to me according to the answers in other forums. - Alan Coromano
  • Service-AarmManager-BroadcastReceiver. Here are three components for your implementation - Android Android
  • @AndroydAndroyd, and more? Why they? Why not ScheduledThreadPoolExecutor? - Alan Coromano

3 answers 3

ScheduledThreadPoolExecutor is a wrapper over ThreadPoolExecutor from java.util.concurrent . With the same success you can use regular Thread .

Service is designed specifically for Android, taking into account the specifics of lifecycle Android applications.

  • so what to choose? service? - Alan Coromano
  • so what to choose? service? - Alan Coromano
  • From the options offered by you - Service. Moreover it will be the simplest implementation. But in terms of efficiency, I am more inclined to alarm manager. It has the main advantage that it will start even if the process of your application is killed. In addition, I have not tested how the services behave on Android 6.0 with the new DOZE battery saver system, but there is a suspicion that it can put out a constantly running service. - anber

In general, the plan is as follows: use BroadcastReceiver to autoload the application when you turn on the phone, read about it here , and use the AlarmManager every 10 seconds for background sending, read about it here . In summary, when you start the phone, you will receive an intent in the BroadcastReceiver , and from there you will start the AlarmManager and the pilaf is ready!

  • above, I was advised to use the service and I, too, were inclined to it. Is it not necessary to use it anywhere at all in my case? - Alan Coromano
  • one
    Honestly, it all depends on the task itself and the design solution of the person who is programming it all, if you really need it, then do it through services, it’s all up to the task, but on your question I see myself as a solution to your problem. Just to understand what is better, you need to read about all 3 classes and understand their essence, then think what you want and decide what you like best from this set - BORSHEVIK

You can use BroadcastReceiver to launch the application when you turn on the phone. And Foreground Service, in which there is a timertusk that performs something once N times.

  • one
    What is foreground service? - Alan Coromano
  • @MariusKavansky is such a service that will always work, even if Activiti is killed, even if there is not enough memory. - researcher
  • Is not all services like that? - Alan Coromano
  • @MariusKavansky No - BORSHEVIK