Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Create Foreground Services

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

Learn about background restrictions, and foreground Services which display a notification to users.

Contributors

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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)
}

Reviews

Comments