Skip to content

How to Prevent Foreground Service Crash After Permission Removal on Android #320

@GiantChipmunk

Description

@GiantChipmunk

Issue:
If the user revokes the necessary permissions (e.g., FOREGROUND_SERVICE_HEALTH, ACTIVITY_RECOGNITION) via device settings and then returns to the app, the app crashes with the following error:

java.lang.SecurityException: Starting FGS with type health callerApp=ProcessRecord{...} targetSDK=35 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_HEALTH] any of the permissions allOf=false [android.permission.ACTIVITY_RECOGNITION, android.permission.BODY_SENSORS, android.permission.HIGH_SAMPLING_RATE_SENSORS]

Steps to Reproduce:

  1. Start the foreground service in the app.
  2. Go to Settings > Apps > MyApp > Permissions and revoke necessary permissions (ACTIVITY_RECOGNITION, FOREGROUND_SERVICE_HEALTH).
  3. Return to the app.
  4. The app crashes immediately with the error mentioned above.

Is there any way to prevent the app from crashing when the user revokes these permissions while the foreground service is running? Ideally, I would like to catch this scenario and gracefully stop the service before Android forces the crash.

Thank you for your help!

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