gpt4 book ai didi

android - 无法恢复 Activity : not allowed to start service Intent, 应用程序在后台

转载 作者:行者123 更新时间:2023-12-05 00:16:28 24 4
gpt4 key购买 nike

我自己无法重现此崩溃,但我最近在 Crashlytics 中看到了很多崩溃。崩溃仅发生在 Android 9 及更高版本上:

Fatal Exception: java.lang.RuntimeException
Unable to resume activity {co.whitesmith.flicks/co.whitesmith.flicks.media.audio.AudioPlayerActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.devbrackets.android.playlistcore.start_service cmp=co.whitesmith.flicks/.media.service.MediaService (has extras) }: app is in background uid UidRecord{63e190 u0a221 TPSL bg:+3h19m42s918ms idle change:idle procs:1 seq(2385,2385,2385)}`

堆栈跟踪:

android.app.ContextImpl.startServiceCommon (ContextImpl.java:1616)
android.app.ContextImpl.startService (ContextImpl.java:1571)
android.content.ContextWrapper.startService (ContextWrapper.java:669)
arrow_right
com.devbrackets.android.playlistcore.manager.BasePlaylistManager.play (BasePlaylistManager.kt:298)
co.whitesmith.flicks.media.audio.AudioPlayerFragment.startPlayback (AudioPlayerFragment.kt:231)
co.whitesmith.flicks.media.audio.AudioPlayerFragment.restartAudio (AudioPlayerFragment.kt:201)
co.whitesmith.flicks.media.audio.AudioPlayerFragment.updateCurrenPlaybackInformation (AudioPlayerFragment.kt:401)
co.whitesmith.flicks.media.audio.AudioPlayerFragment.onResume (AudioPlayerFragment.kt:118)
androidx.fragment.app.Fragment.performResume (Fragment.java:2649)

我不明白这次崩溃。为什么无法在 onResume() 方法中启动服务?当 Activity 进入 Resumed 状态时不应该在前台吗?

最佳答案

这是一个错误!在 Android 9+ 中,应用程序 UID 转向前台和正在恢复的 Activity 之间存在竞争。

这是 Google 的解决方法:

The issue has been addressed in future Android release.

There is a workaround to avoid application crash. Applications can get the process state in Activity.onResume() by calling ActivityManager.getRunningAppProcesses() and avoid starting Service if the importance level is lower than ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.

然而,有一个更简单的解决方法可以延迟服务的启动,直到应用程序完全转为前台:

new Handler().postDelayed(() -> startService(...), 100);

关于android - 无法恢复 Activity : not allowed to start service Intent, 应用程序在后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60038145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com