androidkotlinspotify

Spotify Android SDK authorization only works if Spotify app is active


I'm trying to integrate the Spotify SDK into my Kotlin application, for which I'm following Spotify's Getting started guide. I'm using the "built-in auth flow" just like in their tutorial, this is my code:

override fun onStart() {
        super.onStart()
        val connectionParams = ConnectionParams.Builder(client_id)
            .setRedirectUri(resources.getString(redirect_uri))
            .showAuthView(true)
            .build()

        SpotifyAppRemote.connect(this, connectionParams,
            object : Connector.ConnectionListener {
                override fun onConnected(appRemote: SpotifyAppRemote) {
                    spotifyAppRemote = appRemote
                    Log.d("Spotify", "Connected! Yay!")
                }

                override fun onFailure(throwable: Throwable) {
                    Log.e("Spotify", "Failed to connect to Spotify: $throwable")
                }
            }
        )

The problem is that this authorization only works if the Spotify app was active (in the foreground? sry, i don't know the correct android terminology) right before I start my own app. If I start my app and the Spotify app is not active, then the authorization does not work. The auth view dialog (where the user would log in) does not pop up, neither onConnected nor onFailure are called.

If I had to guess, I'd say that it fails to show the auth view dialog itself, but I'm not sure how to approach this issue.

Edit: I'm using Android 14


Solution

  • Probably no longer relevant for you, but this has to do with the Android 14 itself. I also had an issue with this and saw this message in Logcat:

    ActivityTaskManager  system_server  E  Background activity launch blocked!
    

    There are other threads based on the above information that provide solutions.