Android Background Processing

Sep 23 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk 2021.2.1

Part 3: Use Android Services

19. Create Foreground Services

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. Challenge - Services Next episode: 20. Communicate Using BroadcastReceivers

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 19. Create Foreground Services

The student materials have been reviewed and are updated as of SEPTEMBER 2022.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

As you’ve learned so far, services in Android Oreo and higher are restricted.

const val NOTIFICATION_CHANNEL_NAME = "Synchronize service channel"
const val NOTIFICATION_CHANNEL_ID = "Synchronize ID"

class SynchronizeImagesService : Service() {

  private val remoteApi by lazy { App.remoteApi }

  override fun onBind(intent: Intent?): IBinder? = null

  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    showNotification()

    clearStorage()
    fetchImages()

    return START_NOT_STICKY
  }
}
const val NOTIFICATION_CHANNEL_NAME = "Synchronize service channel"
const val NOTIFICATION_CHANNEL_ID = "Synchronize ID"

class SynchronizeImagesService : Service() {

  ...
  private fun showNotification() {
    createNotificationChannel()
    val notificationIntent = Intent(this, MainActivity::class.java).apply {
      flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
    }

    val pendingIntent = PendingIntent.getActivity(this,
        0, notificationIntent, 0)

    val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setContentTitle("Synchronization service")
        .setContentText("Downloading images")
        .setContentIntent(pendingIntent)
        .build()

    startForeground(1, notification)
  }
  ...
}
const val NOTIFICATION_CHANNEL_NAME = "Synchronize service channel"
const val NOTIFICATION_CHANNEL_ID = "Synchronize ID"

class SynchronizeImagesService : Service() {

  ...

  private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      val serviceChannel = NotificationChannel(
          NOTIFICATION_CHANNEL_ID,
          NOTIFICATION_CHANNEL_NAME,
          NotificationManager.IMPORTANCE_DEFAULT
      )
      val manager = getSystemService(NotificationManager::class.java)
      manager?.createNotificationChannel(serviceChannel)
    }
  }

  ...
}
  fun queueImagesForDownload(context: Context, images: Array<String>) {
    if (images.isNotEmpty()) {
      images.forEach { imageUrl ->
        val file = File(context.externalMediaDirs.first(), imageUrl)

        downloadImage(file, imageUrl)
      }
    }
  }
private fun synchronizeImages() {
  val intent = Intent(requireContext(), SynchronizeImagesService::class.java)
  activity?.startService(intent)
}