AlarmScheduler is a tool to schedule time exact tasks, even in very short period of time.
AlarmScheduler provides various supports:
- Easy-to-use API
- Alarms survive after device reboot
- debuggable with built-in logger
AlarmScheduler is built on top of AlarmManager + Kotlin Coroutines + Room and ❤️ !!
Add the dependency to your module's build.gradle
file:
dependencies {
implementation 'com.carterchen247:alarm-scheduler:x.x.x'
}
To see the complete usage of AlarmScheduler, please build and refer to the demo app.
AlarmScheduler.setAlarmTaskFactory(object : AlarmTaskFactory {
override fun createAlarmTask(alarmType: Int): AlarmTask {
return MyAlarmTask()
}
})
class MyAlarmTask : AlarmTask {
override fun onAlarmFires(alarmId: Int, dataPayload: DataPayload) {
// do something with dataPayload you set
}
companion object {
const val TYPE = 1
}
}
val config = AlarmConfig(
Date().time + 10000L, // trigger time
MyAlarmTask.TYPE
) {
dataPayload("reminder" to "have a meeting")
}
AlarmScheduler.schedule(config){ ... }
AlarmScheduler.schedule(config) { result: ScheduleResult ->
when (result) {
is ScheduleResult.Success -> {
// alarm scheduling success!
}
is ScheduleResult.Failure -> {
when (result) {
ScheduleResult.Failure.CannotScheduleExactAlarm -> {
// handle scenarios like user disables exact alarm permission
}
is ScheduleResult.Failure.Error -> {
// handle error
}
}
}
}
}
When receiving a CannotScheduleExactAlarm
failure result, it means the user disable the exact alarm permission. Try to use openExactAlarmSettingPage()
extension function with prompts to guide your user.
fun Activity.openExactAlarmSettingPage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
startActivity(Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM))
}
}
get notified once user grants the exact alarm permission.
AlarmSchedulerEventObserver { event ->
if (event is ScheduleExactAlarmPermissionGrantedEvent) {
// get notified
}
}
If something is not working, enable the built-in logger to see what’s going on.
AlarmScheduler.setLogger(AlarmSchedulerLogger.DEBUG)
AlarmScheduler.getScheduledAlarmsAsync { scheduledAlarms: List<AlarmInfo> ->
// list the scheduled alarms
}
There are more! refer to AlarmSchedulerContract see full APIs.
internal interface AlarmSchedulerContract {
fun setAlarmTaskFactory(alarmTaskFactory: AlarmTaskFactory)
fun setLogger(loggerImpl: AlarmSchedulerLogger)
fun setErrorHandler(errorHandler: AlarmSchedulerErrorHandler)
fun isAlarmTaskScheduled(alarmId: Int): Boolean
fun cancelAlarmTask(alarmId: Int)
fun cancelAllAlarmTasks()
fun getScheduledAlarmsAsync(callback: ScheduledAlarmsCallback)
fun addEventObserver(observer: AlarmSchedulerEventObserver)
fun removeEventObserver(observer: AlarmSchedulerEventObserver)
fun schedule(config: AlarmConfig, callback: ScheduleResultCallback?)
fun canScheduleExactAlarms(): Boolean
}
✅ Tested
By using AlarmScheduler, when the app recovers from the stopped state, it will automatically detect the ACTION_BOOT_COMPLETED
action, and alarms will be automatically rescheduled.
✅ Handled
To handle the behavior change in Android 14, use ScheduleResultCallback
when calling AlarmScheduler.schedule()
. This callback will return one of the following results:
Success
: The alarm was successfully scheduled.Failure.CannotScheduleExactAlarm
: The app doesn't have permission to schedule exact alarms.Failure.Error
: An error occurred during scheduling.
Here's how to use it:
AlarmScheduler.schedule(config) { result: ScheduleResult ->
when (result) {
is ScheduleResult.Success -> {
// Alarm scheduling success!
}
is ScheduleResult.Failure -> {
when (result) {
ScheduleResult.Failure.CannotScheduleExactAlarm -> {
// Handle scenarios like user disables exact alarm permission
// Consider prompting the user to grant permission
}
is ScheduleResult.Failure.Error -> {
// Handle other errors
}
}
}
}
}
For Android 12 and above, you can use the openExactAlarmSettingPage()
extension function to guide users to the exact alarm permission settings:
fun Activity.openExactAlarmSettingPage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
startActivity(Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM))
}
}
You can also get notified when the user grants the exact alarm permission:
AlarmSchedulerEventObserver { event ->
if (event is ScheduleExactAlarmPermissionGrantedEvent) {
// Permission granted, you can try scheduling the alarm again
}
}
MIT License
Copyright (c) 2020 Carter Chen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.