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 trigger the sync when ever the app is closed / send to background.
Under iOS this is quite easy:
- Listen to
applicationDidEnterBackground:
in the AppDelegate - Use
beginBackgroundTaskWithName:
to start you long running background task and to avoid being suspended while the task is still running
How to do this on Android?
First problem is, that there is nothing equivalent to applicationDidEnterBackground:
. All solution I found so far propose to use the main Activities onPause()
method. However this called any time the main Activity
is paused, which is also the case when another Activity
is started within the app. The is true for the onActivityPaused()
method from ActivityLifecycleCallbacks
interface.
So, how to detect that the app (not just an Activity
) is closed or send to the background?
Second problem is, that I did not find any information on how to run background task. All solutions point to simply start an AsyncTask
, but is this really the correct solution?
AsyncTask
will start a new task, but will this task also run, when the app is closed/inactive? What do I have to do, to prevent the app and the task from being suspended after a few seconds? How can I make sure, that the task can complete, before the app is completely suspended?
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.
Following code will help you to post your content when your app is closed or in background:
More explanation of my code is:
Service runs in background even if your application is in background but remember it always runs on main thread because of which I created a separate thread to perform your background operations.:
Service is started as sticky one as even if because of any reason your service got destroyed in background it will automatically get restarted.
More details can be found here: https://developer.android.com/guide/components/services.html
And to check if your app is in foreground/Background following code will help:
Happy Coding!!!!
The accepted answer is not correct.
Unfortunately, author believes that posting a new Runnable task to the Handler() creates a separate thread - "I created a separate thread to perform your background operations" :
onStartCommand() callback is always called on the main thread, and the new Handler() is attached to current thread (which is "main"). So the Runnable task is posted at the end of main thread queue.
To fix the issue and execute the Runnable in a really separate thread you can use AsyncTask as the simplest solution:
Think yourself before you copy-past anything...