gpt4 book ai didi

Android 12 ForegroundServiceStartNotAllowedException 在前台?

转载 作者:行者123 更新时间:2023-12-04 23:39:19 40 4
gpt4 key购买 nike

虽然这里似乎有一些关于 Android 12 服务限制的问题,但我还没有看到一个特定于这个用例的问题。
背景:我正在启动一个前台服务来处理一个运行时间很长的后台媒体播放器(Exoplayer)。由于应用程序的设置,我无法使用 exoplayer 的内置通知管理器。但是,我看到 Crashlytics 的奇怪行为。我收到 ForegroundServiceStartNotAllowedException而应用程序无疑处于前台。根据日志,很容易看到用户在 startForeground 的一秒钟内浏览应用程序。称呼。
我也在听

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event)
以确保应用程序处于前台状态。
logs
crash
我开始想不出什么会导致这种情况。从时间戳可以看出,不到 1 秒就过去了。该应用程序没有使用案例,服务甚至可能意外地从后台启动。
谢谢
编辑:
我还声明了我的服务类型:
        <service
android:name=".service.SoundPlayerService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaPlayback"
android:stopWithTask="false">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
        if (!audioFocusTakenBackground && Application.instance.isAppInForeground) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
ONGOING_NOTIFICATION_ID,
builder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
startForeground(
ONGOING_NOTIFICATION_ID,
builder.build()
)
}
}
编辑:@Arlind 建议的设备状态选项卡
Device States

最佳答案

Application.instance.isAppInForeground
这个变量有正确的值吗?也许这个变量得到了错误的值。你能检查一下吗?

关于Android 12 ForegroundServiceStartNotAllowedException 在前台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70711950/

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