Search code examples
androidgoogle-glass

Glass: Best way to upload files in background?


What is the best way to upload large files in the background of an Android/Glass application? I'm currently triggering an asynch task for each file (photos / videos) I want to upload, but if the asynch task crashes or the activity that launched it hits an exception the file upload fails.

Is there an android or Glass design pattern to store files in an application-specific directory and have a task that constantly works to make sure those files are uploaded then removed from that folder?


Solution

  • Depending on your requirements you should either use a background service (as @straya mentioned) https://developer.android.com/training/run-background-service/create-service.html

    Or you can use a SyncAdapter: http://developer.android.com/training/sync-adapters/creating-sync-adapter.html

    The first approach you would use if you need to upload those files as soon as possible, the second approach you would use if you want to bundle up several files together and let the operating system decide when to sync them with the server (you can specify how often you'd like your app to synchronize).

    I don't know the requirements of you application but sounds like the second approach is what you need. That is what google is using for many applications like gmail, etc. It is way harder to implement then the background service, but it's worth it. Android is smart about the Sync Adapters, it bundles up several adapters together when possible to preserve the battery consumption.