Question

I have a service which plays music and an activity which provides the GUI for interacting with the service.The activity opens on list item click(I have a list of recordings) and it binds the service(and create it) at onCreate() method.

When onDestroy() is called, I unbind the service (this will destroy the service) - this should be OK since I do not want the service to run if the activity is exited, but the problem appear on orientation change because it re-creates the activity again and the service too(and the track is stopped and played again from the beginning when rotating the device).

I know about some flags (orientationChange) that might be useful, but is not a good practice for me since I want a different layout on landscape. Also I could make the music player service to run as long as my app runs, but isn't a good idea since the user may not want to open the player, but want just to record, so the player service isn't necessarily here.

Here are some code snippets:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LocalBroadcastManager.getInstance(this).registerReceiver(mLocalReceiver, new IntentFilter(PlayerBroadcastReceiver.ACTION_PLAYER_SERVICE_STARTED));
        setContentView(R.layout.media_player_screen);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        AudioPlayerServiceBridge.getInstance().addCallback(this);
        AudioPlayerServiceBridge.getInstance().doBindService(this);

        init(savedInstanceState);       
        super.onCreate(savedInstanceState);
    }

@Override
protected void onDestroy() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalReceiver);
    mLocalReceiver.removeCallback();
    Log.d(AudioPlayerActivity.class.getName(), "onDestroy() -> "+AudioPlayerActivity.class.getName());
    AudioPlayerServiceBridge.getInstance().doUnbindService(this);
    AudioPlayerServiceBridge.getInstance().removeCallback(this);

    super.onDestroy();
}

and the service connection manager:

public void doBindService(Context context) {
    // Establish a connection with the service.  We use an explicit
    // class name because there is no reason to be able to let other
    // applications replace our component.
    if(!mIsBound){
        context.bindService(new Intent(context, 
                AudioPlayerService.class), serviceConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }
}

public void doUnbindService(Context context) {
    if (mIsBound) {
        // If we have received the service, and hence registered with
        // it, then now is the time to unregister.
        if (mServiceMessenger != null) {
            Message msg = Message.obtain(null, AudioPlayerService.MSG_UNREGISTER_CLIENT);
            msg.replyTo = mMessenger;
            mServiceMessenger.send(msg);
        }

        // Detach our existing connection.
        context.unbindService(serviceConnection);
        mIsBound = false;
    }
}

Please show me if possible a good practice to handle this problem.

Was it helpful?

Solution

The answer is:

I should start the service with : startService(new Intent(this, service.class)) AND START BINDING after that. This method prevent the service to be killed when doUnbind() is called. So the onCreate() method is changed now in:

@Override
protected void onCreate(Bundle savedInstanceState) {
    LocalBroadcastManager.getInstance(this).registerReceiver(mLocalReceiver, new IntentFilter(PlayerBroadcastReceiver.ACTION_PLAYER_SERVICE_STARTED));
    setContentView(R.layout.media_player_screen);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    if(savedInstanceState == null)
        startService(new Intent(this, AudioPlayerService.class));

    AudioPlayerServiceBridge.getInstance().addCallback(this);
    AudioPlayerServiceBridge.getInstance().doBindService(this);

    init(savedInstanceState);       
    super.onCreate(savedInstanceState);
}

onDestroy() method:

@Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalReceiver);
        mLocalReceiver.removeCallback();
        Log.d(AudioPlayerActivity.class.getName(), "onDestroy() -> "+AudioPlayerActivity.class.getName());
        AudioPlayerServiceBridge.getInstance().doUnbindService(this);
        AudioPlayerServiceBridge.getInstance().removeCallback(this);

        super.onDestroy();
    }

and stop the service(if you want) in onBackPressed():

@Override
public void onBackPressed() {
    Log.d(AudioPlayerActivity.class.getName(), "onBackPressed() -> "+AudioPlayerActivity.class.getName());
    isPaused = true;
    Log.d(AudioPlayerActivity.class.getName(), "Sending message to player service: MSG_RELEASE_PLAYER");
    AudioPlayerServiceBridge.getInstance().sendAsyncCall(AudioPlayerService.MSG_RELEASE_PLAYER);

    if(mSeekBarChanger != null){
        mSeekBarChanger.stopThread();
    }       

    AudioPlayerServiceBridge.getInstance().doUnbindService(this);
    stopService(new Intent(this, AudioPlayerService.class));
    super.onBackPressed();      
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top