Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingForegroundServiceTypeException on Android 14 (SDK 34) When Using BaseSensorRecordingService #9

Closed
Mustafa-raja opened this issue Sep 3, 2024 · 2 comments

Comments

@Mustafa-raja
Copy link

I'm currently using the backgroundsensors library in my Android project, and I've encountered an issue when running the app on devices with Android 14 (SDK 34). The issue occurs when trying to start a foreground service using BaseSensorRecordingService.

Issue:

Starting from Android 14, the system requires foreground services to specify a foregroundServiceType when calling startForeground. However, it appears that the BaseSensorRecordingService does not include this type when it starts the foreground service, resulting in the following exception:

java.lang.RuntimeException: Unable to create service es.uji.geotec.backgroundsensors.service.BaseSensorRecordingService: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{d9c7d10 8482:com.example.phone2/u0a199} targetSDK=34
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4989)
...
Caused by: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{d9c7d10 8482:com.example.phone2/u0a199} targetSDK=34
at android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:53)
...

Steps to Reproduce:

  • Use BaseSensorRecordingService to start a foreground service on a device running Android 14.
  • Observe the MissingForegroundServiceTypeException being thrown.

Proposed Solution:

Modify the runInForegroundWithNotification method in BaseSensorRecordingService to include the foregroundServiceType parameter when calling startForeground, as shown below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(notificationId, notification);
}

This would ensure compatibility with Android 14 and prevent the MissingForegroundServiceTypeException.

@matey97
Copy link
Collaborator

matey97 commented Sep 3, 2024

Hi @Mustafa-raja, thank you for reporting this issue and the proposed solution. I'm currently working on solving this issue and releasing a new version of the library. I'll keep you posted!

@matey97
Copy link
Collaborator

matey97 commented Sep 3, 2024

Hi @Mustafa-raja, I've released the v1.4.0 of the library supporting Android 14. Therefore, after updating to the new version, everything should work as expected. Take in mind that the indexing of new releases might take a while (it should be available in a couple of hours).

Please, tell me if everything works fine once you update to the new version to close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants