0

I'm a complete beginner here and with Android Studio. Essentially, I'm trying to create a piece of code that does periodic checks whilst the app is backgrounded, however I'm unsure as to how to do this. Would I need to use a background service or something else?

I'd really appreciate any help.

Alex
  • 18
  • 4
  • Possible duplicate of [Android - Run in background - Service vs. standard java class](http://stackoverflow.com/questions/3067324/android-run-in-background-service-vs-standard-java-class) – Jyotman Singh Mar 06 '16 at 12:56

1 Answers1

0

First you need a Service. A Service is a piece of code with no UI meant for background tasks, or for long lasting operations that should last between Activities.

Second, you need to poll periodically. That can be done either via an Alarm or via a Thread. An Alarm will occur on the UI thread, a Thread is parallel but you need to take all the normal parallel processing issues into account. A thread is simpler in many ways though as you can just sleep() on it to wait for your next time to do checks. Which is better depends on your usecase.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127