Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

android - RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()

enter image description here

I received this error report through Fabric.io in both Android 8.0 and Android 7.x.

Since it's not just specific classes that fail, I do not know how to handle them.

Please help me if you have any ideas.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Android O, we have a new background limitations. When you're trying to startService(), you will get IlleagalStateException, so now you should use startForegroundService(), but if you start service by this new method, you will get error like on your screenshot. To avoid this exception you have 5 seconds to make startForeground() after startForegroundService(), to notify user, that you're working in background.

So, where is only one way in Android O:

context.startForegroundService(intent)

And in service onCreate() make something like that:

startForeground(1, new Notification());

UPDATE So as i assume, some manufacturers have backport on Android N these new background limitations, that's why you can get same exception in Android N.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...