Update data in the background Periodically (Android)

The main task of my application is to click a button:

  • Sensor data collection
  • Store data in a database
  • Repeat periodically (every X seconds)

I want to do this in the background, and if Main Activitydestroyed, the background move will continue. Therefore, if you delete an action, I want it to still work.

What I'm doing right now is AlarmManagerhaving Intent Serviceto set a periodic record of the data, but whenever I delete Activitybecause they are connected, my application crashes.

I know that there are different ones for starting a background process, but nobody suits me:

  • Service: depends on MainThread, therefore, if MainActivity is destroyed, crashes
  • Intent Service: the one that is being used right now, but as far as possible associated with the AlarmManager, which is associated with the MainActivity when it is destroyed, fails.
  • AsyncTask: The problem is that it is supposed to do one task in the background, and it ends, but that I really want to do this periodically until the user starts to stop. So it is not a matter of time.

Is there any other way to have a background service? Can any of the previously stated be used for my purpose? How do you recommend me to do this?

Thank you for your responses.

+2
source share
1 answer

android:process=":servicename" AndroidManifest.xml. , .

+1

Source: https://habr.com/ru/post/1625454/


All Articles