Skip to content

Commit

Permalink
explicitly declare the foreground service type
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiqima committed Feb 8, 2024
1 parent c368f66 commit 99c45f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<application
android:allowBackup="true"
Expand All @@ -20,6 +21,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync" />
<receiver android:name=".NotificationActionReceiver"/>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.esri.arcgismaps.sample.generateofflinemapusingandroidjetpackworkmanager

import android.content.Context
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
import android.os.Build
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
Expand Down Expand Up @@ -59,10 +61,18 @@ class OfflineJobWorker(private val context: Context, params: WorkerParameters) :
*/
private fun createForegroundInfo(progress: Int): ForegroundInfo {
// create a ForegroundInfo using the notificationId and a new progress notification
return ForegroundInfo(
notificationId,
workerNotification.createProgressNotification(progress)
)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(
notificationId,
workerNotification.createProgressNotification(progress),
FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
} else {
ForegroundInfo(
notificationId,
workerNotification.createProgressNotification(progress)
)
}
}

override suspend fun doWork(): Result {
Expand Down

0 comments on commit 99c45f2

Please sign in to comment.