On 11 September 2018, we made updates to our iOS and Android SDKs. These new SDKs work for both Spotify Free and Spotify Premium users, offer built-in offline support, and more. Want to access the 2014 Android streaming SDK? It is available on GitHub by clicking here.

When an application connects to Spotify client through the SpotifyAppRemote API, it will prevent it from shutting down even if user is not playing anything. This is an expected behavior - we want Spotify to remain active as long as there’s something interacting with it. At the same time the application using the Spotify App Remote SDK should not keep Spotify alive if there’s no need for it, for example if it’s in the background. The Spotify client will take care of the background playback which means you don’t need to do it. In most cases you don’t need to implement your own service to keep the connection alive, it’s enough to connect when user is actually interacting with you app and disconnect when they’re not. We recommend connecting to Spotify in the onStart method of the Activity that shows the playback state and controls, and disconnecting in the onStop method. Do not keep the connection alive when your app is in the background, otherwise Spotify will not be able to shutdown when it’s inactive. We also recommend to check for an existing connection before connecting again - otherwise two connections will be created and keep Spotify running in the background until the app is killed.

@Override
protected void onStart() {
    super.onStart();
    if (mSpotifyAppRemote != null && mSpotifyAppRemote.isConnected()) {
        SpotifyAppRemote.CONNECTOR.disconnect(mSpotifyAppRemote);
    }

    SpotifyAppRemote.CONNECTOR.connect(this, mConnectionParams, mConnectionListener);
}

@Override
protected void onStop() {
    super.onStop();
    if (mSpotifyAppRemote != null && mSpotifyAppRemote.isConnected()) {
        SpotifyAppRemote.CONNECTOR.disconnect(mSpotifyAppRemote);
    }
}

If the connection succeeds you’re good to go. If it fails because user is not logged in or has not authorized your app you can use connection errors to show additional visual feedback to the users such as login button. For details about connection errors se the error handling page. Specifically for cases where user is not logged in to Spotify or they haven’t authorized your app, you can use the Authorization Library to prompt them to log in and approve the required scopes.

Connector.ConnectionListener mConnectionListener = new Connector.ConnectionListener() {
    @Override
    public void onConnected(SpotifyAppRemote spotifyAppRemote) {
        mSpotifyAppRemote = spotifyAppRemote;
        // setup all the things
    }

    @Override
    public void onFailure(Throwable error) {
        if (error instanceof NotLoggedInException || error instanceof UserNotAuthorizedException) {
            // Show login button and trigger the login flow from auth library when clicked
        } else if (error instanceof CouldNotFindSpotifyApp) {
            // Show button to download Spotify
        }
    }
};

@Override
protected void onStart() {
    super.onStart();
    if (mSpotifyAppRemote != null && mSpotifyAppRemote.isConnected()) {
        SpotifyAppRemote.CONNECTOR.disconnect(mSpotifyAppRemote);
    }

    SpotifyAppRemote.CONNECTOR.connect(this, mConnectionParams, mConnectionListener);
}

@Override
protected void onStop() {
    super.onStop();
    if (mSpotifyAppRemote != null && mSpotifyAppRemote.isConnected()) {
        SpotifyAppRemote.CONNECTOR.disconnect(mSpotifyAppRemote);
    }
}