Search code examples
androidandroid-serviceandroid-mediaplayerandroid-automotivemediabrowserservicecompat

How to start a MediaBrowserServiceCompat in Automotive OS?


I am sending a notification from a worker that has an action which should open the media app. In other words, I want to start the MediaBrowserServiceCompat when the action is clicked. I tried this code:

    val serviceIntent = Intent(applicationContext, MusicService::class.java).apply {
        action = MediaBrowserService.SERVICE_INTERFACE
    }
    val servicePendingIntent = PendingIntent.getService(applicationContext, 0, serviceIntent, 0)

    val builder = NotificationCompat.Builder(
        applicationContext,
        CHANNEL_ID
    )
        .addAction(NotificationCompat.Action(R.drawable.icon, "Listen", servicePendingIntent))

Now, it is true that the service is created and started, however, the Automotive app is not open. I compared the created service from when the launcher is clicked with the one created from my side and I found that there are a bunch of connection records in the former that make the difference:

  • ConnectionRecord{f88b7c5 u10 CR automotive.app/.service.MusicService:@176fe3c} binding=AppBindRecord{15f60a3 automotive.app/.service.MusicService:com.manufacturer-company.launcher}
  • ConnectionRecord{8946e6c u10 CR automotive.app/.service.MusicService:@4705b1f} binding=AppBindRecord{61fffa0 automotive.app/.service.MusicService:com.android.car.media}
  • ConnectionRecord{1dee409 u0 CR automotive.app/.service.MusicService:@d3ce110} binding=AppBindRecord{f061b07 automotive.app/.service.MusicService:com.android.systemui}
  • ConnectionRecord{d27082f u10 CR automotive.app/.service.MusicService:@d58060e} binding=AppBindRecord{142c759 automotive.app/.service.MusicService:com.alliance.car.mediasource}

Is there something I can do to mimic the creation of the service as It is done when the launcher is clicked?


Solution

  • I haven't tried it myself, but I believe you should be able to do this using the CAR_INTENT_ACTION_MEDIA_TEMPLATE and CAR_EXTRA_MEDIA_COMPONENT as described here.