SaschaMoll
SaschaMoll

Reputation: 21

How can I access the CarContext in a Android Auto Media App?

I am currently building a Radio App for Android Auto and I simply want to send a notification to the screen when a song gets requested.

I'm trying to send a CarToast but it requires a CarContext:

CarToast.makeText(**CARCONTEXT**, "Song requested!", CarToast.LENGTH_SHORT).show()

My problem is that I can't easily access the CarContext because I have a Media App using an extended MediaBrowserServiceCompat class. In there I haven't found a way to access the CarContext. Normally I could get the CarContext when I create a CarAppService, but I don't know how I could make a Media App with the CarAppService. There is no documentation for something like this at all.

I also tried sending a normal notification using the NotificationCompatBuilder:

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = "My Radio"
        val descriptionText = "My Radio Notifications"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system.
        val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

fun sendNotification(message: String) {
    println("Sending notification: $message")
    var builder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_custom_search)
        .setContentTitle("My Radio")
        .setContentText(message)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    with(NotificationManagerCompat.from(context)) {
        if (ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            return@with
        }

        var notificationId = (0..10000).random()
        notify(notificationId, builder.build())
    }

This works fine for sending a normal notification to the phone but it won't get displayed on the head unit.

Is there any way to access the CarContext in the MediaBrowserServiceCompat or is there any other way to send a notification in Android Auto?

Upvotes: 0

Views: 207

Answers (2)

jsevy
jsevy

Reputation: 321

Well, I had the same need, and the only approach I found that worked was to send a media SessionError with the message I wanted:

private void broadcastControllerMessage(String message)
{
    // send a pop-up message - only available mechanism is for errors...
    SessionError sessionError = new SessionError(
            SessionError.ERROR_UNKNOWN,
            message);

    // Sending the nonfatal error to all controllers...
    mediaSession.sendError(sessionError);
}

This results in a nice little short-lived text bubble being displayed over the Android Auto media browser or player. Definitely in the hack category, unfortunately... Would be nice if the media3 team added something more legitimate, since it's a common need.

Upvotes: 0

Ben Sagmoe
Ben Sagmoe

Reputation: 1447

Yeah, it's expected that you wouldn't be able to create a toast from a media app. As far as sending notifications goes, have you tried sending notifications extended with CarAppExtender? https://developer.android.com/training/cars/apps#display-notifications

Upvotes: 0

Related Questions