Skip to content

Fix Android Auto integration by adding MediaSession and intent filter#19

Merged
j-almenara-r merged 3 commits intomainfrom
copilot/fix-android-auto-integration
Jan 2, 2026
Merged

Fix Android Auto integration by adding MediaSession and intent filter#19
j-almenara-r merged 3 commits intomainfrom
copilot/fix-android-auto-integration

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 2, 2026

Android Auto requires an active MediaSession to recognize and display apps. The current implementation sets sessionToken = null, preventing the app from appearing in Android Auto's app list.

Changes

VoiceNotesMediaBrowserService.kt

  • Create and manage MediaSessionCompat with proper lifecycle (onCreate/onDestroy)
  • Implement MediaSession.Callback stubs for play/pause/stop controls
  • Add debug logging for connection attempts
  • Relax package validation to allow unknown clients (with TODO for production hardening)
  • Add initialization check in onDestroy() to prevent crashes

AndroidManifest.xml

  • Add MEDIA_PLAY_FROM_SEARCH intent filter to MainActivity
  • Set launchMode="singleTop" on MainActivity

Implementation

override fun onCreate() {
    super.onCreate()
    mediaSession = MediaSessionCompat(this, TAG).apply {
        setFlags(FLAG_HANDLES_MEDIA_BUTTONS or FLAG_HANDLES_TRANSPORT_CONTROLS)
        setCallback(object : MediaSessionCompat.Callback() {
            override fun onPlay() { /* handle play */ }
            override fun onPause() { /* handle pause */ }
            override fun onStop() { /* handle stop */ }
        })
        isActive = true
    }
    sessionToken = mediaSession.sessionToken
}

The app should now appear in Android Auto when connected. Debug logs available via adb logcat | grep VoiceNotesMediaBrowser.

Original prompt

Problem

The app is not appearing in Android Auto when connected to a car. This is issue #16: #16

Root Cause Analysis

The current implementation has several issues preventing the app from being displayed in Android Auto:

  1. Missing MediaSession: The VoiceNotesMediaBrowserService sets sessionToken = null, but Android Auto requires an active MediaSession to recognize and display the app.

  2. No Media Playback Intent Filter: The MainActivity doesn't declare it can handle media playback actions, which Android Auto looks for.

  3. Overly Restrictive Package Validation: The service only allows specific hardcoded package names, which may block some Android Auto implementations.

Required Changes

1. Fix VoiceNotesMediaBrowserService.kt

  • Create a proper MediaSession instead of setting sessionToken to null
  • Implement MediaSession callbacks for basic playback controls
  • Make package validation less restrictive (at least for debugging)
  • Add proper lifecycle management (release MediaSession on destroy)
  • Add logging to help debug connection issues
private lateinit var mediaSession: MediaSessionCompat

override fun onCreate() {
    super.onCreate()
    
    // Create MediaSession
    mediaSession = MediaSessionCompat(this, "VoiceNotesSession").apply {
        setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
        setCallback(object : MediaSessionCompat.Callback() {
            override fun onPlay() {
                // Handle play
            }
            
            override fun onPause() {
                // Handle pause
            }
            
            override fun onStop() {
                // Handle stop
            }
        })
        isActive = true
    }
    
    sessionToken = mediaSession.sessionToken
}

override fun onDestroy() {
    mediaSession.release()
    super.onDestroy()
}

2. Update AndroidManifest.xml

Add media playback intent filter to MainActivity:

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- Add this for Android Auto -->
    <intent-filter>
        <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

3. Update app/build.gradle

Ensure MediaSessionCompat is available by verifying dependencies:

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.media:media:1.7.0'
}

The androidx.media:media library already includes MediaSessionCompat, so no additional dependencies should be needed.

Testing Instructions

After these changes:

  1. Clean and rebuild the project
  2. Uninstall the old version from the device
  3. Install the new version
  4. Connect to Android Auto
  5. Check Android Auto app drawer for the app
  6. Use adb logcat | grep -E "MediaBrowser|VoiceNotes" to monitor connection attempts

Expected Outcome

The app should now appear in the Android Auto app list when the phone is connected to a car or Android Auto for Phone Screens.

Closes #16

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 2, 2026 18:05
Co-authored-by: j-almenara-r <246562873+j-almenara-r@users.noreply.github.com>
Co-authored-by: j-almenara-r <246562873+j-almenara-r@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix app visibility issues in Android Auto Fix Android Auto integration by adding MediaSession and intent filter Jan 2, 2026
Copilot AI requested a review from j-almenara-r January 2, 2026 18:07
@j-almenara-r j-almenara-r marked this pull request as ready for review January 2, 2026 18:15
@j-almenara-r j-almenara-r merged commit 42c6b4a into main Jan 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App not displayed in Android Auto

2 participants