# Background Downloads

By default, downloads run in the application process and are not equivalent to Android system-managed background downloads. But, let’s also understand how to configure optional foreground-service support so long-running offline map downloads can continue when the application moves to the background.

To continue long-running transfers after the app enters the background, the host must implement _DownloadForegroundHook_ and provide notification, **Service**, and Android-version compatibility logic.

Register the hook:

```kotlin
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        // Nextbillion and RegionalOffline initialization omitted.
        RegionalOffline.setDownloadForegroundHook(AppDownloadForegroundHook)
    }
}
object AppDownloadForegroundHook : DownloadForegroundHook {
    override fun startForegroundService(context: Context) {
        RegionalDownloadService.start(context)
    }

    override fun stopForegroundService(context: Context) {
        RegionalDownloadService.stop(context)
    }

    override fun isForegroundEstablished(): Boolean =
        RegionalDownloadService.foregroundEstablished
}
```

At minimum, declare the following in the manifest:

```xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application ...>
    <service
        android:name=".RegionalDownloadService"
        android:exported="false"
        android:foregroundServiceType="dataSync" />
</application>
```

The hook's start/stop operations must be idempotent. _isForegroundEstablished()_ must wait until the **Service** has successfully called Android _startForeground()_ before returning true. The application must also handle notification permission, foreground **Service** start timing, and background-start restrictions for the target Android version.

If no hook is registered, transfers are interrupted when the system terminates the application process. Task records are restored the next time the application starts. Tasks that were in-progress, when interrupted, are normalized to a paused state, and the UI should prompt the user to call `RESUME`.
