Blog Infos
Author
Published
Topics
Published
Foreground services

According to the official android documentation, a foreground service performs operations that are noticeable to the user. It shows a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. While this notification is shown, the Android system will be gentle to the application “owning” the notification, giving it the required priority and trying not to stop it, because it’s probably doing some important work.

Context#startService(i: Intent)

you’re using

Context#startForegroundService(i: Intent)

Once the service is started, Android will invoke onStartCommand upon it. Then you will have to take care of displaying the foreground notification, and you do that by calling

Service#startForeground(id: Int, notification: Notification)

If you forget it, or it takes too long before you invoke that method you will end up with an exception

Fatal Exception: android.app.RemoteServiceException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{1228932 u0 lc.st.free/lc.st.notification.StartStopNotificationService}

Swipetimes foreground notification

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

Application crash

Well, I surely understood the technical requirements and implemented it accordingly:

  • Show the notification
  • Do the work in the foreground service asynchronously (more or less)
What’s causing the crash?

 

Photo by mostafa meraji on Unsplash

 

Photo by Jeff Kingma on Unsplash

The solution

Well, the solution was quite straightforward. We need to synchronise with the main thread and start the service only if main becomes free again. So, the only required change is to delegate the service start to a handler which is bound to main.

handler.post {
    context.startForegroundService(...)
}
Conclusion

Well, I know this was quite a non-standard issue. Nonetheless, I found it really interesting. There may be more ways of solving it:

  1. Postpone the service start until the main thread is free. Do this by posting to the main thread’s handler. For my case the preferred approach.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu