Search code examples
spotifyandroidx

Android Unable to open Spotify app programmatically


I have installed Spotify app in my android phone. Below code was not able to launch or open Spotify app programmatically. Please help.

Android Intent:
PackageName: com.spotify.music
ActionName: android.intent.action.VIEW


  val intent = Intent(Intent.action.VIEW) 

  intent.setPackage("com.spotify.music")

if (intent.resolveActivity(packageManager) == null) {
            Toast.makeText(this,"Please install Spotify App first.",Toast.LENGTH_SHORT).show()
            return
        }


  startActivity(intent)

Solution

  • To open any application using the package name, you can follow this: https://stackoverflow.com/a/7574735/4566483.

    For Spotify:

    val launchIntent = packageManager.getLaunchIntentForPackage("com.spotify.music")
    if (launchIntent != null) { 
        startActivity(launchIntent)
    }