Android SyncAdapter Service: Keep alive after onPerformSync ends?
-
05-07-2021 - |
Question
I have setup a SyncAdapter using the Android sample project, Steele's two part tutorial (1 and 2) and the Google I/O 2010 talk on REST applications.
It seems like AbstractThreadedSyncAdapter always calls onDestroy on the SyncAdapter Service immediately after its onPerformSync completes. I would like to spin off other threads from the SyncAdapter service though, and I wonder if it is possible to keep the SyncAdapter Service alive until the those threads complete--for example, to register callbacks from these asynchronous worker threads back to the SyncAdapter service.
Is it possible to do that without killing the service after onPerformSync? Or do you have to put all sync methods into the same thread within the SyncAdapter onPerformSync method?
Solution
I'm pretty sure the answer is "no"... but even if it was possible, why would you want to do this anyway? The SyncAdapter
does its work on a background thread already... so why should you have to fire off separate worker threads within the SyncAdapter
service to perform the sync?
I imagine things will begin to get messy if you attempt to register callbacks and/or mess around with the SyncAdapter
's lifecycle to achieve this. Android's sync service is responsible for scheduling/performing sync operations for any number of apps (i.e. GMail, Google Drive, etc.), so when onPerformSync
finishes, that is a way of telling Android that the work is completely done and that it should focus on the next app it must sync.