I ended up getting this to do what I wanted by using a BoundService
that implements a Messenger
interface (Android Dev Doc). Following this alone still caused the service to destroy itself when the app closed. The key to prevent the service destruction and allow a reconnect is to call startService()
before calling bindService()
. This will keep the service running in the background until my app calls stopService()
(in my case when the user explicitly logs out).
Called in the UploaderActivity.onStart() method:
Intent initService = new Intent( this, UploaderService.class );
startService( initService );
// serviceConnection is a member of UploaderActivity of type
// android.content.ServiceConnection that handles when the service binds.
// This needs to be a class member to unbind() when the activity is destroyed
bindService( initService, serviceConnection, Context.BIND_AUTO_CREATE );