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?

有帮助吗?

解决方案

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
    }
  }
};

其他提示

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 -

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top