AlarmManager 笔记

绑定

1
2
3
4
5
6
7
8
9
10
11
12
private fun useAlarmManager(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, AppWidgetService::class.java)
val pendIntent =
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(),
backgroundCheckTime.toLong() * 60000,
pendIntent
)
}

取消

1
2
3
4
5
6
7
8
private fun cancelAlarmManager(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, AppWidgetService::class.java)
val pendIntent =
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
pendIntent.cancel()
alarmManager.cancel(pendIntent)
}