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


    Android App Developer

    sipgate GmbH
    Düsseldorf, Remote
    • Full Time
    apply now

    Android Build Engineer

    Pinterest
    San Francisco, CA | Seattle, WA
    • Full Time
    apply now

    Senior Mobile Systems SDK Engineer

    Sauce Labs
    Remote
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

,

Leveling Up Your Tests

We all know about TDD and Unit Testing, and even screenshot testing, but sometimes we do not need to embrace a new paradigm to make our tests better. These are several techniques I have adopted…
Watch Video

Leveling Up Your Tests

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
Nowadays authentication has become common in almost all apps. And many of us know…
READ MORE
blog
Collections are a set of interfaces and classes that implement highly optimised data structures.…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu