Skip to content

Crash: USB screen starts its service while the app counts as in the background #189

Closed

Description

@zeevy

Crash reported in the Play Console:

android.app.BackgroundServiceStartNotAllowedException
  at android.content.ContextWrapper.startService
  at in.co.gorest.grblcontroller.UsbConnectionActivity.onStart (UsbConnectionActivity.java:82)

Cause

UsbConnectionActivity.onStart() calls startService() for GrblUsbSerialService. Android refuses a service start while it counts the app as in the background, and it can do that at the first onStart() (for example when the screen turns off as the activity starts). The refusal is not caught, so the app crashes.

The service is already created and kept alive by bindService() in onCreate(), and its onStartCommand() does nothing, so the start can be skipped when Android refuses it.

Fix

Catch IllegalStateException around that startService() call and log a warning. BackgroundServiceStartNotAllowedException (Android 12+) and the older refusal both extend it.

Acceptance

  • When Android refuses the start, the app logs a warning and does not crash. USB connect still works through the bound service.
  • When Android allows the start, nothing changes.

Activity

  1. zeevy commented on Oct 10, 2026

    @zeevy
    OwnerAuthor

    Fixed in PR #190 (branch fix/186-streaming-fgs-start-crash). Reproduced on the emulator with the old code and checked the fix there. Closes when the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions