startForegroundService

startForegroundService

本文介绍了Context.startForegroundService()然后未调用Service.startForeground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将在 MainActivity onCreate 中调用 startForegroundService(intent).然后将 startForeground(ON_SERVICE_CONNECTION_NID,通知)放在 Service onCreate startCommand 中.但是我仍然偶尔会收到此错误.

My app will call startForegroundService(intent) in the onCreate of the MainActivity. And I put startForeground(ON_SERVICE_CONNECTION_NID, notification) in both the onCreate and the startCommand of the Service.But I'm still receiving this error occasionally.

Exception android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1775)
android.os.Handler.dispatchMessage (Handler.java:105)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6541)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

这怎么可能发生? onCreate startCommand 是否有可能在5秒钟内没有被调用?如果是这样,我们应该如何正确调用 startForegroundService ?

How could this happen?Is it possible that the onCreate or startCommand doesn't get called in 5 seconds?If so, how should we call startForegroundService with no error?

更新了2017-12-09

Updated 2017-12-09

我不知道为什么每个人都说这与这个问题相同:您甚至可以在那边找到我的评论.

I don't know why every one said that it's the same as this question: Context.startForegroundService() did not then call Service.startForeground()You can even find a comment from me over there.

它与众不同的原因是您在此问题的第一行可以看到.我已经将 startForegroundService startForeground 放在正确的位置,但是它仍然随机显示此错误.

The reason why it's different is what you can see at the first line of this question. I have put the startForegroundService and startForeground at the right place, but it's still showing this error randomly.

这是Google问题跟踪器: https://issuetracker.google.com/issues/67920140

And Here's the Google issue tracker: https://issuetracker.google.com/issues/67920140

这是有关服务生命周期的另一个问题.服务关闭后,可能会错误地触发该断言.

This is another problem about the lifecycle of the service.The assertion may be incorrectly triggered after the service closed.

推荐答案

我在Galaxy Tab A(Android 8.1.0)上遇到了此问题.在我的应用程序中,我在 MainApplication (应用程序扩展类)的构造函数中启动前台服务.使用调试器,我发现在 startForegroundService(intent)之后到达服务的 OnCreate()花了超过5秒的时间.即使在 OnCreate()中调用了 startForeground(ON_SERVICE_CONNECTION_NID,通知),我也崩溃了.

I experienced this issue for Galaxy Tab A(Android 8.1.0). In my application, I start a foreground service in the constructor of MainApplication(Application-extended class). With the Debugger, I found it took more than 5 second to reach OnCreate() of the service after startForegroundService(intent). I got crash even though startForeground(ON_SERVICE_CONNECTION_NID, notification) got called in OnCreate().

尝试了不同的方法后,我发现以下一项对我有用:

After trying different approach, I found one works for me as the following:

在构造函数中,我使用 AlarmManager 唤醒了接收器,并在接收器中启动了前台服务.

In the constructor, I used AlarmManager to wakeup a receiver, and in the receiver, I started the foreground service.

我猜我之所以有这个问题,是因为应用程序启动的繁重工作延迟了前台服务的创建.

I guessed that the reason I had the issue because heavy workload of application starting delayed the creation of the foreground service.

尝试我的方法,您也可以解决该问题.祝你好运.

Try my approach, you might resolve the issue as well. Good luck.

这篇关于Context.startForegroundService()然后未调用Service.startForeground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 09:20