fix(android): call startForeground before blocking Flic2Manager.init - #61
Conversation
Android 8+ kills Flic2Service when startForeground() is delayed by a slow init. Promote the FGS first, keep init synchronous so bind and button listeners still run after the manager is ready. Co-authored-by: Cursor <cursoragent@cursor.com>
Plain-language overviewGoal: Stop Android from crashing the Flic button service when the app starts that background service. In simple terms: Android requires a visible “this service is running” notification within a few seconds of starting the Flic service. Setting up the Flic library can take longer than that, so the phone killed the app. This change shows the notification first, then finishes Flic setup on the same startup step so button presses are not skipped. What changes for users / the system:
High-level pictureflowchart LR
startService[Android starts Flic service] --> showNotice[Show running notification]
showNotice --> setupFlic[Finish Flic setup]
setupFlic --> deliverClicks[Listen and deliver button clicks]
Last synced to commit |
There was a problem hiding this comment.
Pull request overview
This PR addresses Android 8+ foreground-service timing constraints by promoting Flic2Service to the foreground earlier in onCreate(), reducing the chance the OS kills the service when Flic2Manager.init() blocks long enough to miss the ~5s startForegroundService() → startForeground() deadline.
Changes:
- Reorders
Flic2Service.onCreate()to create the notification channel/notification and callstartForegroundService()before running synchronousFlic2Manager.init(). - Bumps the package version to
2.0.0-beta.29(and updates the lockfile accordingly).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| package.json | Bumps library version to 2.0.0-beta.29. |
| package-lock.json | Updates lockfile version fields to match 2.0.0-beta.29. |
| android/src/main/java/nl/xguard/flic2/Flic2Service.kt | Promotes the service to foreground before potentially slow synchronous manager initialization. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Android 8+ kills Flic2Service when startForeground() is delayed by a
slow init. Promote the FGS first, keep init synchronous so bind and
button listeners still run after the manager is ready.
Co-authored-by: Cursor cursoragent@cursor.com
Note
Low Risk
Small reordering in service onCreate with no auth or data changes; main risk is duplicate or early foreground notification behavior, which onStartCommand already guards.
Overview
Fixes Flic2Service being killed on Android 8+ when
Flic2Manager.init()blocks the main thread long enough to miss the ~5s window afterstartForegroundService().onCreate now creates the notification channel, builds the notification, and calls
startForegroundService()before synchronousFlic2Manager.init(). Init order is unchanged in spirit—still on the main thread in onCreate so bind and button listeners see a ready manager—only foreground promotion is moved earlier.Package version bumped to 2.0.0-beta.29.
Reviewed by Cursor Bugbot for commit 7f75c21. Bugbot is set up for automated code reviews on this repo. Configure here.