Pregunta

in the beginning I thought it would be fairly simple, but I guess it's not. I want to call a URL every 10 minutes, either when the app is in the background or in the foreground. How can I realize this?

¿Fue útil?

Solución

I'd use a Service with a Handler inside. Using directly Threads is another approach but it's more likely it will be killed if the Android OS needs to free memory.

The Handler part would be something like this:

boolean stopHandler = false;

Runnable runnable = new Runnable() {
  @Override
  public void run() {
    // Do whatever you need
    ...

    if (!stopHandler) {
      handler.postDelayed(this, 600000);     // 10 minutes
    }
  }
};

Otros consejos

In iOS 7 you can schedule background operations for periodically fetching data from the network. This tutorial is an example of scheduling background fetch operations -

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top