Reputation: 283
I've developed an application where a Text
appears on screen whenever the current time matches the date and time selected by the user. This is accomplished using Alarm Manager
. The date and time are selected using the built in DatePicker
and TimePicker
composables. However, there is a problem. The Alarm Manager
event only triggers when the current date matches the selected date. It does not account for the time.
Here is a break down of the application. There is a column that contains four Composables
. The first one is a Text
, the other is a DatePicker
, the second one is a TimeInput
, and the last one is a Button
. There is a Broadcast Receiver
called AlarmReceiver
. There is an empty String
variable not located inside of any Class
or Activity
that holds the value for the Text
Composable
.
Here is the code in the MainActivity
. It forms the user interface and holds the logic for the AlarmReceiver
.
// Variable That Holds The Date Picker State
var daterPickState = rememberDatePickerState(
initialSelectedDateMillis = LocalDate.now() // Initial Selected Date Is The Current Date
)
// Variable That Holds The Time Picker State
var timePickerState = rememberTimePickerState(
initialHour = LocalTime.now().hour, // Initial Selected Hour Is The Current Hour
initialMinute = LocalTime.now().minute, // Initial Selected Minute Is The Current Minute
is24Hour = false // The Time Picker Is In AM And PM Format
)
// Variable That Holds Alarm Manager
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
// Variable That Holds Alarm Intent
val alarmIntent = Intent(this, AlarmReceiver::class.java)
// Variable That Holds Pending Intent
val pendingIntent = PendingIntent.getBroadcast(
applicationContext, // Context
0, // Request Code
alarmIntent, // Intent
PendingIntent.FLAG_UPDATE_CURRENT // Flag
)
// Variable That Holds The Selected The Date And Divides It Into Year, Month, And Day
var selectedDate = Instant.ofEpochMilli(datePickState.selectedDateMillis!!)
.atZone(ZoneId.systemDefault()).toLocalDate() // The Values Are Converted Into An Integer
// Variable That Convert The Date And Time Into A Long
var selectedDateTime = LocalDateTime.of(
selectedDate.year, // Year
selectedDate.monthValue, // Month
selectedDate.dayOfMonth, // Day Of The Month
timePickState.hour, // Hour
timePickState.minute // Minute
).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() // Long Conversion Logic
// Layout For The Application
Column(
modifier = Modifier.fillMaxSize(), // Column Fills Entire Screen
horizontalAlignment = Alignment.CenterHorizontally, // Contents Are Centered Horizontally
verticalArrangement = Arrangement.Center // Contents Are Centered Vertically
) {
// Text That Gets Displayed When The Current Date & Time Matches The Set Date & Time
Text(text = text)
// Date Picker
DatePicker(state = datePickerState) // DatePickerState Is Passed Into The State Parameter
// Time Picker
TimeInput(state = timePickerState) TimePickerState Is Passed Into The State Parameter
// Button That Sets The Alarm Manager Event
Button(
onClick = {
// Instance of Alarm Manager
alarmManager.setExact(
AlarmManager.RTC_WAKEUP, // Alarm Type
selectedDateTime, // When To Trigger Alarm Converted In Milliseconds
pendingIntent // Pending Intent
)
}
){ Text(text = "Set Alarm Event") } // Button Text
}
Here is the empty String
variable that holds the value for the Text
that appears when the Alarm Manager
event goes off.
var text by mutableStateOf("")
Here is the Broadcast Receiver
. It is registered in the Android Manifest
file.
// Alarm Broadcast Receiver
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
text = "It Works!" // The Text Is Changed To "It Works!"
}
}
As stated above the Alarm Manager
only triggers at the set date. It ignores the time. This problem only occurs with the DatePicker
and TimePicker
Composables
. If I hard code the date and time into the Alarm Manager
everything works fine. If I only use the TimePicker
and not the DatePicker
it also works fine. That would mean the problem has something to do with the DatePicker
but I don't know what.
Upvotes: 1
Views: 25