У меня есть странная проблема, что мое приложение зависло, когда я открываю его из уведомления, я пробовал много решений, но любое из них, которое работало для меня, пожалуйста, мне нужна помощь, потому что я пытался по этой проблеме более одного месяца и отмечая получить Сработал для меня сценарий для воспроизведения моей проблемы, как это:
1 - Откройте приложение, и служба будет запущена, и уведомление будет отображаться при запуске песни
2 - Приостановить приложение нажав кнопку «Домой», уведомление останется, и игрок продолжит играть
3 - Остановить игрока в уведомлении и отменить уведомление, перетащив его на сайт
4 - Повторно открыть приложение и затем приложение вылетает с этой проблемой
2020-04-07 17:31:03.787 764-780/? E/ActivityManager: ANR in com.playground.mediaservice
PID: 11556
Reason: Context.startForegroundService() did not then call Service.startForeground()
Load: 0.0 / 0.0 / 0.0
CPU usage from 173267ms to 0ms ago (2020-04-07 17:28:10.092 to 2020-04-07 17:31:03.359):
Вот мой код для создания уведомления:
class NotificationBuilder(private val context: Context) {
var songTracker: MutableLiveData<SongWrapper> = MutableLiveData()
private val platformNotificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val playAction = NotificationCompat.Action(
R.drawable.exo_controls_play,
context.getString(R.string.notification_play),
MediaButtonReceiver.buildMediaButtonPendingIntent(context, ACTION_PLAY)
)
private val pauseAction = NotificationCompat.Action(
R.drawable.exo_controls_pause,
context.getString(R.string.notification_pause),
MediaButtonReceiver.buildMediaButtonPendingIntent(context, ACTION_PAUSE)
)
private val stopPendingIntent =
MediaButtonReceiver.buildMediaButtonPendingIntent(context, ACTION_STOP)
fun buildNotification(sessionToken: MediaSessionCompat.Token): Notification {
if (shouldCreateNowPlayingChannel()) {
createNowPlayingChannel()
}
val controller = MediaControllerCompat(context, sessionToken)
val playbackState = controller.playbackState
val builder = NotificationCompat.Builder(context, NOW_PLAYING_CHANNEL)
// Only add actions for skip back, play/pause, skip forward, based on what's enabled.
if (playbackState.isPlaying) {
builder.addAction(pauseAction)
} else if (playbackState.isPlayEnabled) {
builder.addAction(playAction)
}
val mediaStyle = MediaStyle()
.setCancelButtonIntent(stopPendingIntent)
.setMediaSession(sessionToken)
.setShowActionsInCompactView(0)
.setShowCancelButton(true)
val notifyIntent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
putExtras(Bundle().apply {
putParcelable(SELECTED_SONG_FROM_NOTI, songTracker.value)
})
}
val notifyPendingIntent = PendingIntent.getActivity(
context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
val bitmapIcon = BitmapFactory.decodeResource(context.resources, R.drawable.logo)
return builder.setContentIntent(notifyPendingIntent)
.setContentText(songTracker.value?.currentSong?.itemName)
.setDeleteIntent(stopPendingIntent)
.setLargeIcon(bitmapIcon)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setSmallIcon(R.drawable.dancer)
.setStyle(mediaStyle)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build()
}
}
и вот где я получил намерение в MediaService
private inner class MediaControllerCallback : MediaControllerCompat.Callback() {
override fun onMetadataChanged(metadata: MediaMetadataCompat?) {
mediaController.playbackState?.let {
serviceScope.launch {
updateNotification(it)
}
}
}
override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
state?.let {
serviceScope.launch {
updateNotification(it)
}
}
}
private fun updateNotification(playbackState: PlaybackStateCompat) {
val updatedState = playbackState.state
Log.i(LOG_MSG, " -------------- start of updateNotification ${playbackState.stateName} -------------------------")
// Skip building a notification when state is "none" and metadata is null.
val notification = if (mediaController.metadata != null
&& updatedState != PlaybackStateCompat.STATE_NONE
) {
notificationBuilder.buildNotification(mediaSession.sessionToken)
} else {
null
}
Log.i(LOG_MSG, "before of when statement with notification : $notification")
when (updatedState) {
PlaybackStateCompat.STATE_BUFFERING,
PlaybackStateCompat.STATE_PLAYING -> {
Log.i(
LOG_MSG,
"updated state is EITHER playing or buffering therefor i am in FIRST state : ${playbackState.stateName}"
)
becomingNoisyReceiver.register()
if (notification != null) {
notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification)
Log.i(LOG_MSG, "isForegroundService is : $isForegroundService")
if (!isForegroundService) {
ContextCompat.startForegroundService(
applicationContext,
Intent(applicationContext, this@MusicService.javaClass)
)
startForeground(NOW_PLAYING_NOTIFICATION, notification)
isForegroundService = true
Log.i(
LOG_MSG,
"i called startForegroundService() and startForeground() and now isForegroundService : $isForegroundService"
)
}
}
}
else -> {
Log.i(
LOG_MSG,
"updated state is NEITHER playing or buffering therefor i am in SECOND state : ${playbackState.stateName}"
)
becomingNoisyReceiver.unregister()
Log.i(LOG_MSG, "isForegroundService is : $isForegroundService")
if (isForegroundService) {
stopForeground(false)
isForegroundService = false
Log.i(
LOG_MSG,
"i called stopForeground() and now isForegroundService() : $isForegroundService"
)
// If playback has ended, also stop the service.
if (updatedState == PlaybackStateCompat.STATE_NONE) {
Log.i(
LOG_MSG,
"updated state is NONE therefor i called stopSelf() : ${playbackState.stateName}"
)
stopSelf()
}
if (notification != null) {
notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification)
} else {
Log.i(
LOG_MSG,
"notification was null therefore i will call removeNowPlayingNotification"
)
removeNowPlayingNotification()
}
}
else if(updatedState == PlaybackStateCompat.STATE_NONE){
stopSelf()
}
}
}
}
}
private fun removeNowPlayingNotification() {
stopForeground(true)
}
и я получаю этот журнал:
2020-04-07 17:28:47.565 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_NONE -------------------------
2020-04-07 17:28:47.565 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : null
2020-04-07 17:28:47.565 11556-11556/com.playground.mediaservice I/fehler_log: updated state is NEITHER playing or buffering therefor i am in SECOND state : STATE_NONE
2020-04-07 17:28:47.566 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : false
2020-04-07 17:28:47.567 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_NONE -------------------------
2020-04-07 17:28:47.567 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : null
2020-04-07 17:28:47.567 11556-11556/com.playground.mediaservice I/fehler_log: updated state is NEITHER playing or buffering therefor i am in SECOND state : STATE_NONE
2020-04-07 17:28:47.567 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : false
2020-04-07 17:29:37.261 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_BUFFERING -------------------------
2020-04-07 17:29:37.276 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : Notification(channel=com.playground.mediaservice.utils.media.NOW_PLAYING pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x18 color=0x00000000 category=transport actions=1 vis=PUBLIC)
2020-04-07 17:29:37.276 11556-11556/com.playground.mediaservice I/fehler_log: updated state is EITHER playing or buffering therefor i am in FIRST state : STATE_BUFFERING
2020-04-07 17:29:37.283 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : false
2020-04-07 17:29:37.287 11556-11556/com.playground.mediaservice I/fehler_log: i called startForegroundService() and startForeground() and now isForegroundService : true
2020-04-07 17:29:37.297 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_BUFFERING -------------------------
2020-04-07 17:29:37.306 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : Notification(channel=com.playground.mediaservice.utils.media.NOW_PLAYING pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x18 color=0x00000000 category=transport actions=1 vis=PUBLIC)
2020-04-07 17:29:37.306 11556-11556/com.playground.mediaservice I/fehler_log: updated state is EITHER playing or buffering therefor i am in FIRST state : STATE_BUFFERING
2020-04-07 17:29:37.313 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : true
2020-04-07 17:29:39.591 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_PLAYING -------------------------
2020-04-07 17:29:39.605 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : Notification(channel=com.playground.mediaservice.utils.media.NOW_PLAYING pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x18 color=0x00000000 category=transport actions=1 vis=PUBLIC)
2020-04-07 17:29:39.605 11556-11556/com.playground.mediaservice I/fehler_log: updated state is EITHER playing or buffering therefor i am in FIRST state : STATE_PLAYING
2020-04-07 17:29:39.612 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : true
2020-04-07 17:30:54.633 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_PAUSED -------------------------
2020-04-07 17:30:54.648 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : Notification(channel=com.playground.mediaservice.utils.media.NOW_PLAYING pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x18 color=0x00000000 category=transport actions=1 vis=PUBLIC)
2020-04-07 17:30:54.648 11556-11556/com.playground.mediaservice I/fehler_log: updated state is NEITHER playing or buffering therefor i am in SECOND state : STATE_PAUSED
2020-04-07 17:30:54.649 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : true
2020-04-07 17:30:54.654 11556-11556/com.playground.mediaservice I/fehler_log: i called stopForeground() and now isForegroundService() : false
2020-04-07 17:30:58.430 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_NONE -------------------------
2020-04-07 17:30:58.435 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : null
2020-04-07 17:30:58.435 11556-11556/com.playground.mediaservice I/fehler_log: updated state is NEITHER playing or buffering therefor i am in SECOND state : STATE_NONE
2020-04-07 17:30:58.435 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : false
2020-04-07 17:30:58.439 11556-11556/com.playground.mediaservice I/fehler_log: -------------- start of updateNotification STATE_NONE -------------------------
2020-04-07 17:30:58.442 11556-11556/com.playground.mediaservice I/fehler_log: before of when statement with notification : null
2020-04-07 17:30:58.442 11556-11556/com.playground.mediaservice I/fehler_log: updated state is NEITHER playing or buffering therefor i am in SECOND state : STATE_NONE
2020-04-07 17:30:58.443 11556-11556/com.playground.mediaservice I/fehler_log: isForegroundService is : false
2020-04-07 17:31:03.404 764-780/? I/zygote64: libdebuggerd_client: started dumping process 11556
2020-04-07 17:31:03.405 620-620/? I//system/bin/tombstoned: registered intercept for pid 11556 and type kDebuggerdJavaBacktrace
2020-04-07 17:31:03.419 11556-11562/com.playground.mediaservice I/zygote64: Thread[3,tid=11562,WaitingInMainSignalCatcherLoop,Thread*=0x72f82bf400,peer=0x13580378,"Signal Catcher"]: reacting to signal 3
2020-04-07 17:31:03.575 764-11234/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.playground.mediaservice/.components.MainActivity (has extras)} from uid 10048
2020-04-07 17:31:03.717 9837-11287/? I/PBSessionCacheImpl: Deleted sessionId[43265169802962973] from persistence.
2020-04-07 17:31:03.743 9837-9868/? W/SearchServiceCore: Abort, client detached.
2020-04-07 17:31:03.746 9837-9868/? I/MicroDetectionState: Should stop hotword detection immediately - false
2020-04-07 17:31:03.756 9837-9868/? I/MicroDetectionState: Should stop hotword detection immediately - false
2020-04-07 17:31:03.784 11556-11556/com.playground.mediaservice I/MainActivity: onStateNotSaved
2020-04-07 17:31:03.784 620-620/? I//system/bin/tombstoned: received crash request for pid 11556
2020-04-07 17:31:03.785 620-620/? I//system/bin/tombstoned: found intercept fd 512 for pid 11556 and type kDebuggerdJavaBacktrace
2020-04-07 17:31:03.785 11556-11556/com.playground.mediaservice I/MainActivity: onRestart
2020-04-07 17:31:03.785 11556-11562/com.playground.mediaservice I/zygote64: Wrote stack traces to '[tombstoned]'
2020-04-07 17:31:03.785 11556-11556/com.playground.mediaservice I/MainActivity: onStart
2020-04-07 17:31:03.786 764-780/? I/zygote64: libdebuggerd_client: done dumping process 11556
2020-04-07 17:31:03.787 764-780/? E/ActivityManager: ANR in com.playground.mediaservice
PID: 11556
Reason: Context.startForegroundService() did not then call Service.startForeground()
Load: 0.0 / 0.0 / 0.0
CPU usage from 173267ms to 0ms ago (2020-04-07 17:28:10.092 to 2020-04-07 17:31:03.359):
5.1% 764/system_server: 2.7% user + 2.4% kernel / faults: 17531 minor 8 major
0.2% 573/media.codec: 0.1% user + 0% kernel / faults: 1377 minor
4.2% 430/surfaceflinger: 2% user + 2.1% kernel / faults: 172 minor 1 major
2.4% 9936/com.android.vending: 2.1% user + 0.2% kernel / faults: 16054 minor 14 major
2.2% 8293/com.google.android.gms: 1.8% user + 0.3% kernel / faults: 30018 minor 63 major
1.6% 561/audioserver: 0.8% user + 0.7% kernel / faults: 418 minor 1 major
2% 1118/com.android.systemui: 1.4% user + 0.5% kernel / faults: 7982 minor 8 major
1.2% 9837/com.google.android.googlequicksearchbox:search: 1% user + 0.1% kernel / faults: 21164 minor 31 major
1.1% 486/mdss_fb0: 0% user + 1.1% kernel
0.8% 1774/com.google.android.gms.persistent: 0.6% user + 0.2% kernel / faults: 9792 minor 10 major
0.8% 9468/kworker/u12:2: 0% user + 0.8% kernel
0.6% 263/cfinteractive: 0% user + 0.6% kernel
0.6% 10/rcu_preempt: 0% user + 0.6% kernel
0.6% 41/kworker/0:1: 0% user + 0.6% kernel
0.6% 411/kworker/u12:4: 0% user + 0.6% kernel
0.5% 269/mmcqd/0: 0% user + 0.5% kernel
0.5% 9668/kworker/u12:1: 0% user + 0.5% kernel
0.4% 53/kworker/u13:0: 0% user + 0.4% kernel
0.4% 468/irq/215-fc38800: 0% user + 0.4% kernel
0.4% 297/msm-core:sampli: 0% user + 0.4% kernel
0.3% 9513/kworker/3:0: 0% user + 0.3% kernel
0.3% 9549/kworker/1:2: 0% user + 0.3% kernel
0.3% 1273/VosMCThread: 0% user + 0.3% kernel
0.3% 3021/com.google.android.googlequicksearchbox: 0.2% user + 0% kernel / faults: 8334 minor 10 major
0.3% 3/ksoftirqd/0: 0% user + 0.3% kernel
0.3% 11197/com.google.android.apps.gcs: 0.2% user + 0% kernel / faults: 3143 minor
0.2% 15/ksoftirqd/1: 0% user + 0.2% kernel
0.2% 2848/adbd: 0% user + 0.2% kernel / faults: 2336 minor
0.2% 166/hwrng: 0% user + 0.2% kernel
0.2% 440/kworker/u12:5: 0% user + 0.2% kernel
0.2% 8970/com.android.chrome:sandboxed: 0.2% user + 0% kernel / faults: 2209 minor 7 major
0.2% 176/kgsl_worker_thr: 0% user + 0.2% kernel
0.2% 10870/com.google.android.youtube: 0.1% user + 0% kernel / faults: 3012 minor 1 major
0.2% 10395/com.google.android.apps.messaging: 0.1% user + 0% kernel / faults: 5166 minor 6 major
0.1% 427/msm_irqbalance: 0% user + 0.1% kernel / faults: 1 minor
0.1% 10492/transport: 0% user + 0.1% kernel / faults: 118 minor
0.1% 360/logd: 0% user + 0% kernel / faults: 21 minor
0.1% 135/kswapd0: 0% user + 0.1% kernel
0.1% 423/android.hardware.wifi@1.0-service: 0% user + 0.1% kernel / faults: 48 minor
0.1% 20/ksoftirqd/2: 0% user + 0.1% kernel
0% 57/irq/51-cpr: 0% user + 0% kernel
0.1% 160/vsync_retire_wo: 0% user + 0.1% kernel
0.1% 2866/com.google.process.gservices: 0% user + 0% kernel / faults: 1112 minor
0.1% 9517/kworker/2:2: 0% user + 0.1% kernel
0% 25/ksoftirqd/3: 0% user + 0% kernel
0% 9429/kworker/u13:6: 0% user + 0% kernel
0% 543/jbd2/dm-2-8: 0% user + 0% kernel
0% 529/dmcrypt_write: 0% user + 0% kernel
0% 9511/kworker/u13:1: 0% user + 0% kernel
0% 9532/kworker/u13:2: 0% user + 0% kernel
0% 10235/com.google.android.apps.walletnfcrel: 0% user + 0% kernel / faults: 1554 minor 1 major
0% 8/rcuc/0: 0% user + 0% kernel
0% 212/irq/504-synapti: 0% user + 0% kernel
0% 10157/android.process.acore: 0% user + 0% kernel / faults: 577 minor
0% 268/nanohub: 0% user + 0% kernel
0% 429/lmkd: 0% user + 0% kernel / faults: 1 minor
0% 1265/wlan_logging_th: 0% user + 0% kernel
0% 9687/kworker/u13:8: 0% user + 0% kernel
0% 553/perfd: 0% user + 0% kernel / faults: 119 minor
0% 557/thermal-engine: 0% user + 0% kernel / faults: 6 minor
0% 13/rcuc/1: 0% user + 0% kernel
0% 361/servicemanager: 0% user + 0% kernel / faults: 2 minor
0% 425/rmt_storage: 0% user + 0% kernel / faults: 61 minor 5 major
0% 572/wificond: 0% user + 0% kerne
2020-04-07 17:31:03.789 764-780/? I/ActivityManager: Killing 11556:com.playground.mediaservice/u0a247 (adj 0): bg anr