Skip to content

Commit

Permalink
start service as a foreground service
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Feb 27, 2025
1 parent ea52c67 commit bb6a24f
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 5 deletions.
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<uses-feature
android:name="android.hardware.camera"
Expand Down Expand Up @@ -131,7 +132,10 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<service android:name=".datamanager.MyDownloadService"
android:exported="false" />
<service
android:name=".datamanager.MyDownloadService"
android:foregroundServiceType="dataSync"
android:permission="android.permission.FOREGROUND_SERVICE"
android:exported="false"/>
</application>
</manifest>
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml.lite
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<uses-feature
android:name="android.hardware.camera"
Expand Down Expand Up @@ -131,7 +132,10 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<service android:name=".datamanager.MyDownloadService"
android:exported="false" />
<service
android:name=".datamanager.MyDownloadService"
android:foregroundServiceType="dataSync"
android:permission="android.permission.FOREGROUND_SERVICE"
android:exported="false"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.ole.planet.myplanet.datamanager

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.ResponseBody
import org.ole.planet.myplanet.MainApplication.Companion.createLog
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.model.Download
import org.ole.planet.myplanet.model.RealmMyLibrary
import org.ole.planet.myplanet.utilities.FileUtils.availableExternalMemorySize
Expand Down Expand Up @@ -48,6 +51,8 @@ class MyDownloadService : Service() {
preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

startForegroundServiceWithNotification()

val urlsKey = intent?.getStringExtra("urls_key") ?: "url_list_key"
val urlSet = preferences.getStringSet(urlsKey, emptySet()) ?: emptySet()

Expand All @@ -69,6 +74,20 @@ class MyDownloadService : Service() {
return START_STICKY
}

private fun startForegroundServiceWithNotification() {
val channelId = "DownloadChannel"
val channel = NotificationChannel(channelId, "Download Service", NotificationManager.IMPORTANCE_LOW)
notificationManager?.createNotificationChannel(channel)

notificationBuilder = NotificationCompat.Builder(this, channelId)
.setContentTitle(getString(R.string.downloading_files))
.setContentText(getString(R.string.preparing_download))
.setSmallIcon(R.drawable.ic_download)
.setProgress(100, 0, true)

startForeground(1, notificationBuilder!!.build())
}

private fun initDownload(url: String, fromSync: Boolean) {
val retrofitInterface = ApiClient.client?.create(ApiInterface::class.java)
try {
Expand Down Expand Up @@ -256,7 +275,7 @@ class MyDownloadService : Service() {
putExtra("urls_key", urlsKey)
putExtra("fromSync", fromSync)
}
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">عرض الحقول الإضافية</string>
<string name="hide_additional_fields">إخفاء الحقول الإضافية</string>
<string name="age">العمر</string>
<string name="preparing_download">جارٍ تحضير التنزيل…</string>
<string name="downloading_files">جارٍ تنزيل الملفات</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">mostrar campos adicionales</string>
<string name="hide_additional_fields">ocultar campos adicionales</string>
<string name="age">Edad</string>
<string name="preparing_download">Preparando la descarga…</string>
<string name="downloading_files">Descargando archivos</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">afficher les champs supplémentaires</string>
<string name="hide_additional_fields">masquer les champs supplémentaires</string>
<string name="age">Âge</string>
<string name="preparing_download">Préparation du téléchargement…</string>
<string name="downloading_files">Téléchargement des fichiers</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ne/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">थप क्षेत्रहरू देखाउनुहोस्</string>
<string name="hide_additional_fields">थप क्षेत्रहरू लुकाउनुहोस्</string>
<string name="age">उमेर</string>
<string name="preparing_download">डाउनलोडको तयारी गर्दै…</string>
<string name="downloading_files">फाइलहरू डाउनलोड हुँदैछन्</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-so/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">muuji beeraha dheeraadka ah</string>
<string name="hide_additional_fields">qari beeraha dheeraadka ah</string>
<string name="age">Da\'da</string>
<string name="preparing_download">Diyaarinaya soo dejinta…</string>
<string name="downloading_files">Faylasha ayaa la soo dejinayaa</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,5 +1242,7 @@
<string name="show_additional_fields">show additional fields</string>
<string name="hide_additional_fields">hide additional fields</string>
<string name="age">Age</string>
<string name="preparing_download">Preparing download…</string>
<string name="downloading_files">Downloading files</string>

</resources>

0 comments on commit bb6a24f

Please sign in to comment.