[Android] AlarmManager

πŸ’‘μš°μ•„ν•œν…Œκ·Έμ½”μŠ€ λ―Έμ…˜μ„ μ§„ν–‰ν•˜λ©΄μ„œ ν™œμš©ν•œ AlarmManager에 λŒ€ν•΄μ„œ μƒμ„Έν•˜κ²Œ κΈ°λ‘ν•˜μ˜€μŠ΅λ‹ˆλ‹€!

 

AlarmManger

  • AlarmManagerλŠ” Androidμ—μ„œ νŠΉμ • μ‹œκ°„μ— μž‘μ—…μ„ μ˜ˆμ•½ν•˜κ³  μ‹€ν–‰ν•  수 μžˆλ„λ‘ λ„μ™€μ£ΌλŠ” μ„œλΉ„μŠ€μž…λ‹ˆλ‹€.
  • 앱이 λ°±κ·ΈλΌμš΄λ“œ μƒνƒœμ΄κ±°λ‚˜ μ’…λ£Œλœ μƒνƒœμ—μ„œλ„ μ§€μ •λœ μ‹œκ°„μ— μž‘μ—…μ„ μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 정해진 μ‹œκ°„μ— μ•Œλ¦Όμ„ λ³΄λ‚΄κ±°λ‚˜, 주기적인 μž‘μ—…μ„ μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

νŠΉμ§•

  • 정해진 μ‹œκ°„μ— μž‘μ—… μ‹€ν–‰
    • νŠΉμ • μ‹œκ°„μ— ν•œ 번 μž‘μ—…μ„ μ‹€ν–‰ν•˜κ±°λ‚˜, 주기적으둜 λ°˜λ³΅λ˜λŠ” μž‘μ—…μ„ μ˜ˆμ•½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ‹€ν–‰
    • 앱이 μ‹€ν–‰ 쀑이지 μ•Šλ”λΌλ„ μ•ŒλžŒμ„ μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • μ ˆμ „ λͺ¨λ“œμ—μ„œ μ‹€ν–‰ κ°€λŠ₯
    • AlarmManagerλŠ” μ ˆμ „ λͺ¨λ“œμ—μ„œλ„ μ‹€ν–‰λ˜λ„λ‘ μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 배터리 νš¨μœ¨μ„±
    • Doze λͺ¨λ“œ(잠자기)λ‚˜ μ•± λŒ€κΈ° λͺ¨λ“œμ—μ„œ μ•ŒλžŒμ΄ μ§€μ—°λ˜κ±°λ‚˜ μ‹€ν–‰λ˜μ§€ μ•Šλ„λ‘ μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
      • Android API 23λΆ€ν„° λ„μž…λœ λͺ¨λ“œμ—μ„œλŠ” AlarmManagerκ°€ λ™μž‘ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

PendingIntent

  • Pending은 보λ₯˜μ€‘μ΄λΌλŠ” 뜻으둜 μ§€κΈˆ λ‹Ήμž₯ μ‚¬μš©ν•˜μ§€ μ•Šκ³ , μΆ”ν›„ νŠΉμ • μ΄λ²€νŠΈμ—μ„œ λ°œλ™λ˜λŠ” intentμž…λ‹ˆλ‹€.
  • μ™ΈλΆ€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ κΆŒν•œμ„ ν—ˆκ°€ν•˜μ—¬ μ•ˆμ— λ“€μ–΄μžˆλŠ” Intentλ₯Ό 마치 본인 μ•±μ˜ ν”„λ‘œμ„ΈμŠ€μ—μ„œ μ‹€ν–‰ν•˜λŠ” κ²ƒμ²˜λŸΌ μ‚¬μš©ν•˜κ²Œ ν•˜λ©° λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ— μœ„μž„μ„ μ²˜λ¦¬ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.
  • 주둜 AlarmManager와 ν•¨κ»˜ μ‚¬μš©ν•©λ‹ˆλ‹€.

μ•ŒλžŒ 등둝

μΌνšŒμ„± μ•ŒλžŒ

  • νŠΉμ • μ‹œκ°„μ— ν•œ 번 μ‹€ν–‰λ˜λŠ” μ•ŒλžŒμ„ λ“±λ‘ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • PendingIntentλ₯Ό ν™œμš©ν•˜μ—¬ μ•ŒλžŒμ΄ λ°œμƒν•  λ•Œ μˆ˜ν–‰ν•  μž‘μ—…μ„ μ •μ˜ν•©λ‹ˆλ‹€.
  • μ•ŒλžŒμ΄ λ°œμƒν•˜λ©΄ AlarmManager.RTC_WAKEUPλ₯Ό 톡해 κΈ°κΈ°λ₯Ό 깨우고, setExact()λ₯Ό 톡해 지정 된 μ‹œκ°„μ— ν•œ 번만 μ‹€ν–‰ν•˜λŠ” μ•ŒλžŒμ„ λ“±λ‘ν•©λ‹ˆλ‹€.
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

// AlarmManagerκ°€ μ‹€ν–‰ν•  PendingIntent
val intent = Intent(this, MyBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

// μ•ŒλžŒμ„ μ„€μ •ν•  μ‹œκ°„ (ν˜„μž¬ μ‹œκ°„ + 10초)
val triggerTime = System.currentTimeMillis() + 10 * 1000

// μΌνšŒμ„± μ•ŒλžŒ μ„€μ •
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)

반볡 μ•ŒλžŒ

  • setRepeating()을 톡해 반볡 μ‹€ν–‰λ˜λŠ” μ•ŒλžŒμ„ μ„€μ •ν•©λ‹ˆλ‹€.
  • AlarmManager.INTERVAL_FIFTEEN_MINUTESλ₯Ό ν™œμš©ν•΄ μ•ŒλžŒμ„ 반볡 μ‹€ν–‰ν•˜λ„λ‘ μ„€μ •ν•©λ‹ˆλ‹€.
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

// AlarmManagerκ°€ μ‹€ν–‰ν•  PendingIntent
val intent = Intent(this, MyBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

// μ•ŒλžŒμ„ μ„€μ •ν•  μ‹œκ°„ (5λΆ„λ§ˆλ‹€ 반볡)
val interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES
val triggerTime = System.currentTimeMillis() + interval

// 반볡 μ•ŒλžŒ μ„€μ •
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, interval, pendingIntent)
  • 반볡 μ•ŒλžŒμ€ 배터리 효율 문제둜 ꢌμž₯ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  • setExact()λ‚˜ setExactAndAllowWhileIdle() 등을 μ μ ˆν•˜κ²Œ ν™œμš©ν•˜λŠ” 방식도 μ’‹μŠ΅λ‹ˆλ‹€.

μ •ν™•ν•œ μ•ŒλžŒ

  • setExactAndAllowWhileIdle()λ₯Ό ν™œμš©ν•΄μ„œ Doze λͺ¨λ“œλ‚˜ μ•± λŒ€κΈ° λͺ¨λ“œμ—μ„œλ„ μ •ν™•ν•œ μ•ŒλžŒμ„ 보낼 수 μžˆμŠ΅λ‹ˆλ‹€.
  • 이 λ©”μ„œλ“œλŠ” μ‹œμŠ€ν…œμ΄ μ ˆμ „ λͺ¨λ“œμΌ λ•Œλ„ μ•ŒλžŒμ΄ μ •ν™•ν•˜κ²Œ μ‹€ν–‰λ˜λ„λ‘ ν•©λ‹ˆλ‹€.
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, MyBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

// μ •ν™•ν•œ μ‹œκ°„μ— μ•ŒλžŒ μ„€μ •
val triggerTime = System.currentTimeMillis() + 10 * 1000
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)

μ•ŒλžŒ ν•΄μ œ

  • cancel() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ•ŒλžŒμ„ μ„€μ •ν•  λ•Œ μ‚¬μš©ν–ˆλ˜ PendingIntent와 λ™μΌν•œ 것을 전달할 수 μžˆμŠ΅λ‹ˆλ‹€.
// μ•ŒλžŒ μ·¨μ†Œ
alarmManager.cancel(pendingIntent)

BroadcastReceiver와 μ‚¬μš©

  • BroadcastReceiverλŠ” μ•ŒλžŒμ΄ λ°œμƒν–ˆμ„ λ•Œ 이벀트λ₯Ό μ²˜λ¦¬ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.
  • AlarmManager둜 μ„€μ •λœ μ•ŒλžŒμ΄ λ°œμƒν•˜λ©΄, μ‹œμŠ€ν…œμ€ PendingIntentλ₯Ό 톡해 μ„€μ •λœ BroadcastReceiverλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.
class MyBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        // μ•ŒλžŒ λ°œμƒ μ‹œ μ²˜λ¦¬ν•  μž‘μ—…
        Toast.makeText(context, "Alarm Triggered!", Toast.LENGTH_SHORT).show()
    }
}

정리

  • AlarmManagerλŠ” μ•ˆλ“œλ‘œμ΄λ“œμ—μ„œ μ§€μ •λœ μ‹œκ°„μ— μž‘μ—…μ„ μ˜ˆμ•½ν•˜κ³ , 앱이 λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ‹€ν–‰ 쀑일 λ•Œλ„ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆλ„λ‘ λ„μ™€μ€λ‹ˆλ‹€.
  • Doze λͺ¨λ“œλ‚˜ 배터리 μ‚¬μš©λŸ‰μ— μ£Όμ˜ν•΄μ•Όν•˜λ©°, μΌμ •ν•œ κ°„κ²©μœΌλ‘œ λ°˜λ³΅λ˜λŠ” μž‘μ—…μ„ μ²˜λ¦¬ν•  λ•ŒλŠ” WorkManagerκ°€ 적합할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

[Android, Kotlin] WorkManger둜 맀일 νŠΉμ • μ‹œκ°„μ— λ°±κ·ΈλΌμš΄λ“œ μž‘μ—… μ‹€ν–‰

WorkManagerλ₯Ό μ‚¬μš©ν•œ λ°±κ·ΈλΌμš΄λ“œ μž‘μ—… - Kotlin  |  Android Developers