yassineAbou / Clock

A Jetpack Compose clock app with timer, stopwatch and one-time/recurring alarms.
Apache License 2.0
29 stars 7 forks source link

Get error with targetSdk 34 #2

Closed sakaijunsoccer closed 8 months ago

sakaijunsoccer commented 8 months ago

This app is amazing! Now, when I set this code to buid.gradle.kts with target sdk 34, I get the following error: In Android 14, it seems that I have to specify FOREGROUND_SERVICE and tags in AndroidManifest.xml, but I don't know which service name to specify. Is it possible for you to let me know if you don't mind?

android.app.InvalidForegroundServiceTypeException: Starting FGS with type none callerApp=ProcessRecord{af3b637 6882:io.alamoa.clock/u0a42} targetSDK=34 has been prohibited
yassineAbou commented 8 months ago

Sorry for the delayed response; I've been away from my PC in the past days. I'm happy that you enjoy the app - it's very meaningful to me.

I've noticed the app crashes on Android 14 due to a foreground service issue.

Here's how to fix it:

Add this line to your manifest: <service android:name="androidx.work.impl.foreground.SystemForegroundService" android:foregroundServiceType="mediaPlayback" tools:node="merge" />

In the alarmWorker, stopwatchWorker, timerCompletedWorker, and TimerRunningWorker classes, update the foreground service type like this:

val foregroundInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ForegroundInfo(
                    ...,
                    ...,
                    ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK

                )
            } else {
                ForegroundInfo(
                    ...,
                    ...,

                )
            }

For guidance, visit the "Add a foreground service type to a long-running worker" section : here If this solves the problem, please fork from the master branch, implement these changes, and submit a pull request. I'll make sure to add you as a contributor.