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.

Schedule Work With AlarmManager

The student materials have been reviewed and are updated as of SEPTEMBER 2022. Keep in mind that we now need to add an extra flag to our pending intent to make it work with the latest versions of Android. You also need an extra permission to create exact alarms.

As the last legacy concept, use the AlarmManager to create a signal to wake up the device and update an existing Activity.

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.

After learning about the AsyncTask and JobScheduler, it’s time for the AlarmManager!

val alarmManager = getSystemService(AlarmManager::class.java) ?: return
val currentTime = System.currentTimeMillis()
    val alarmManager = getSystemService(AlarmManager::class.java) ?: return
    val currentTime = System.currentTimeMillis()

    val pendingIntent = PendingIntent.getActivity(
        this,
        0,
        Intent(this, MainActivity::class.java).apply {
          flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
        },
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE //Update Note: Adds mutability flag needed when targeting Android 31+
    )
    alarmManager.setExact(
        AlarmManager.RTC_WAKEUP,
        currentTime + TimeUnit.MILLISECONDS.toSeconds(10_000),
        pendingIntent
    )
  override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
  }
  override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    Thread(Runnable {
      val imageUrl = URL("https://wallpaperplay.com/walls/full/1/c/7/38027.jpg")
      val connection = imageUrl.openConnection() as HttpURLConnection
      connection.doInput = true
      connection.connect()
    }).start()
  }
  override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    Thread(Runnable {
      val imageUrl = URL("https://wallpaperplay.com/walls/full/1/c/7/38027.jpg")
      val connection = imageUrl.openConnection() as HttpURLConnection
      connection.doInput = true
      connection.connect()

      val inputStream = connection.inputStream
      val bitmap = BitmapFactory.decodeStream(inputStream)

      runOnUiThread { image.setImageBitmap(bitmap) }
    }).start()
  }

Reviews

Comments