Fix MissingForegroundServiceTypeException in TimerService

This commit is contained in:
Joseph 2024-02-29 13:29:52 +00:00
parent fd15d10028
commit 12f906bfc3
2 changed files with 16 additions and 2 deletions

View File

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
@ -48,6 +49,11 @@
<service
android:name=".TimerService"
android:exported="false" />
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="App does not require SCHEDULE_EXACT_ALARM or USE_EXACT_ALARM, but needs foreground service for foreground timer."/>
</service>
</application>
</manifest>

View File

@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
import android.media.AudioAttributes
import android.media.MediaPlayer
import android.net.Uri
@ -76,7 +77,14 @@ class TimerService : Service() {
secondsLeft = (intent?.getIntExtra("milliseconds", 0) ?: 0) / 1000
currentDescription = intent?.getStringExtra("description").toString()
secondsTotal = secondsLeft
startForeground(ONGOING_ID, getProgress(secondsLeft).build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(ONGOING_ID, getProgress(secondsLeft).build(), FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
} else
{
startForeground(ONGOING_ID, getProgress(secondsLeft).build())
}
battery()
Log.d("TimerService", "onStartCommand seconds=$secondsLeft")