我创建了一个实现 MediaBrowserServiceCompat 的测试应用程序。我已遵循本指南: https ://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice 创建了 MediaPlaybackService 和 MainActivity。我在 onDestroy 方法中添加了泄漏金丝雀并添加了 AppWatcher.objectWatcher.watch(this)。在打开和退出应用程序时,leak canary 发现一个泄漏:
6153 bytes retained
┬
├─ android.service.media.MediaBrowserService$ServiceBinder
│ Leaking: UNKNOWN
│ GC Root: Global variable in native code
│ ↓ MediaBrowserService$ServiceBinder.this$0
│ ~~~~~~
├─ androidx.media.MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26
│ Leaking: UNKNOWN
│ MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26 does not wrap an activity context
│ ↓ MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26.mBase
│ ~~~~~
╰→ com.example.mediabrowsertestapp.MediaPlaybackService
Leaking: YES (ObjectWatcher was watching this)
MediaPlaybackService does not wrap an activity context
key = 11f40383-1498-4743-9f20-208cbd2839a1
watchDurationMillis = 5191
retainedDurationMillis = 183
Please include this in bug reports and Stack Overflow questions.
Build.VERSION.SDK_INT: 28
Build.MANUFACTURER: HMD Global
LeakCanary version: 2.0
App process name: com.example.mediabrowsertestapp
Analysis duration: 8967 ms
Heap dump file path: /data/user/0/com.example.mediabrowsertestapp/files/leakcanary/2019-12-10_10-21-47_693.hprof
Heap dump timestamp: 1575969720525
由于该应用程序仅包含来自 google 示例的代码,因此我无法弄清楚如何处理此泄漏。我应该忽略它吗?
代码: https ://github.com/finneapps/MediaBrowserService-memory-leak
package com.example.mediabrowsertestapp
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.media.MediaBrowserServiceCompat
import leakcanary.AppWatcher
private const val LOG_TAG = "MediaPlaybackService"
class MediaPlaybackService : MediaBrowserServiceCompat() {
private var mediaSession: MediaSessionCompat? = null
private lateinit var stateBuilder: PlaybackStateCompat.Builder
override fun onCreate() {
super.onCreate()
mediaSession = MediaSessionCompat(baseContext, LOG_TAG).apply {
setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
)
stateBuilder = PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY
or PlaybackStateCompat.ACTION_PLAY_PAUSE
)
setPlaybackState(stateBuilder.build())
setSessionToken(sessionToken)
}
}
override fun onGetRoot(
clientPackageName: String, clientUid: Int,
rootHints: Bundle?
): BrowserRoot? {
return BrowserRoot(LOG_TAG, null)
}
override fun onLoadChildren(
parentMediaId: String,
result: Result<List<MediaBrowserCompat.MediaItem>>
) {
result.sendResult(emptyList())
}
override fun onDestroy() {
super.onDestroy()
AppWatcher.objectWatcher.watch(this)
}
}
package com.example.mediabrowsertestapp
import android.content.ComponentName
import android.media.AudioManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.PlaybackStateCompat
class MainActivity : AppCompatActivity() {
private val controllerCallback = object : MediaControllerCompat.Callback() {
override fun onMetadataChanged(metadata: MediaMetadataCompat?) {}
override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {}
}
private lateinit var mediaBrowser: MediaBrowserCompat
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mediaBrowser = MediaBrowserCompat(
this,
ComponentName(this, MediaPlaybackService::class.java),
connectionCallbacks,
null
)
}
override fun onStart() {
super.onStart()
mediaBrowser.connect()
}
override fun onResume() {
super.onResume()
volumeControlStream = AudioManager.STREAM_MUSIC
}
override fun onStop() {
super.onStop()
MediaControllerCompat.getMediaController(this)?.unregisterCallback(controllerCallback)
mediaBrowser.disconnect()
}
private val connectionCallbacks = object : MediaBrowserCompat.ConnectionCallback() {
override fun onConnected() {
mediaBrowser.sessionToken.also { token ->
val mediaController = MediaControllerCompat(
this@MainActivity, // Context
token
)
MediaControllerCompat.setMediaController(this@MainActivity, mediaController)
}
}
override fun onConnectionSuspended() {
}
override fun onConnectionFailed() {
}
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.mediabrowsertestapp"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.media:media:1.1.0"
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}