0

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?

1 Answers1

0

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.

Ben Sagmoe
  • 440
  • 2
  • 9
  • Q1: Do I need to include the android.car in my project in order to achieve this, or just using the constants values is ok? Q2: Do you imply that my project needs to have 30 as a min SDK(CAR_EXTRA_MEDIA_COMPONENT)? Does this mean it is impossible to accomplish this feature in APIs 29 and 28? – Ahmed Sellami Oct 24 '22 at 13:21
  • > Do I need to include the android.car in my project in order to achieve this, or just using the constants values is ok? Using the raw string value is OK. > Do you imply that my project needs to have 30 as a min SDK(CAR_EXTRA_MEDIA_COMPONENT)? Does this mean it is impossible to accomplish this feature in APIs 29 and 28? For API level 28, I believe you may need to set [CAR_EXTRA_MEDIA_PACKAGE](https://cs.android.com/android/platform/superproject/+/master:packages/services/Car/car-lib/src/android/car/Car.java;l=1132?q=CAR_EXTRA_MEDIA_PACKAGE) instead. – Ben Sagmoe Oct 27 '22 at 16:41
  • Solution: `val notificationIntent = Intent(android.car.Car.CAR_INTENT_ACTION_MEDIA_TEMPLATE) notificationIntent.putExtra("android.car.intent.extra.MEDIA_COMPONENT", ComponentName(applicationContext, MusicService::class.java).flattenToString())` – Ahmed Sellami Nov 28 '22 at 14:23