远程服务异常:Context.startForegroundService()未调用Service.startForeground()。

11

enter image description here

我在Android 8.0和Android 7.x中通过Fabric.io收到了这个错误报告。

由于不仅是特定类失败,我不知道如何处理它们。

如果您有任何想法,请帮助我。

1个回答

7
在Android O中,我们有一个新的后台限制。当你试图使用startService()时,你会得到一个IlleagalStateException异常,所以现在你应该使用startForegroundService(),但是如果你通过这种新的方法启动服务,你会像屏幕截图上那样出现错误。为了避免这个异常,在startForegroundService()之后你有5秒钟时间来使用startForeground()通知用户,你正在后台工作。
所以,在Android O中只有一种方法:
context.startForegroundService(intent)

在服务的onCreate()方法中,可以像以下这样创建一个对象:

startForeground(1, new Notification());

更新 我猜测一些制造商已经在Android N上回溯了这些新的后台限制,这就是为什么您在Android N中可能会遇到相同的异常。


@Hoang Duc Tuan,在Android N_MR1中,你实际上不能使用startForegroundService。也许你可以发布一些代码? - HeyAlex
我的代码:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } - Hoang Duc Tuan
3
我在 MainActivityonCreate 中启动了一个服务,并在服务的 onCreateonStartCommand 中调用了 startForeground。当应用程序调用 onCreate 时,它应始终处于前台状态。但是我仍然收到这个错误。 - Kimi Chiu
@Kimi Chiu,这是在Android O中吗? - HeyAlex
3
在 Android 版本 27 中,使用 new Notification() 会抛出一个错误。现在你需要提供一个有效的通知。 - suku
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接