Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 349
Hoang Duc Tuan Avatar asked Sep 23 '17 02:09

Hoang Duc Tuan


People also ask

When to call startForeground?

After the system creates the service, the app has 5 seconds to call the service's startForeground() method to display the user-visible notification for the new service. If the app does not call startForeground() within this time limit, the system will stop the service and declare the application as ANR.

How do you stop foreground service?

The service must stop itself by calling stopSelf(), or another component can stop it by calling stopService(). Once requested to stop with stopSelf() or stopService(), the system destroys the service as soon as possible.

What is the difference between startService and startForegroundService?

startForegroundService(Intent) instead of the former startService(Intent) . The service must then call startForeground(int, Notification) within first 5 seconds after it has started, otherwise system will stop the service.


1 Answers

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.

like image 51
HeyAlex Avatar answered Oct 01 '22 16:10

HeyAlex