How to execute background task when Android app is closed / set to background?

后端 未结 4 467
再見小時候
再見小時候 2020-12-03 01:47

My Android 4+ app is connected to a custom web service that is used to sync data every few minutes. To make sure, that the online data is always up to date, I want to trigge

4条回答
  •  没有蜡笔的小新
    2020-12-03 02:06

    You can use services for what you want to achieve. A service will keep on running in the background even when the activity component has been destroyed, provided you have not invoked stopService(..) or stopSelf() methods.

    In the service you can make an Async network call, preferably using retrofit, and then you can update you local storage like Sqlite DB with the latest data fetched from your web service.

    Here is the official link, you can just use an unbounded service for what you want to achieve.

提交回复
热议问题