I’ve developed a test application in Android. When launched it displays a notification using alarm manager. I’d like to have the notification appear at a specific time in the future using a formatted time selected by the user using the new TimePicker
composable, however I can’t figure out how to implement that. The alarm manager requires that all time to trigger the alarm must be converted to a long.
Here is a breakdown of the application. The apps consists of a blank white screen, no UI elements what so ever. The only thing that happens is a notification saying “It works!” appears as soon as the app starts.
Here is a breakdown of the Main Activity
. There are four variables and one instance of a variable. One for the alarm manager, one for the alarm intent used in the broadcast receiver, one for the pending intent, and one for the alarm time that is set to LocalDateTime.now().minute
. There is also an instance of the alarm manager that defines the alarm manager parameters.
Here is the code for the Main Activity
.
// 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(
this,
0,
alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
// Variable That Holds Alarm Time
val alarmTime = LocalDateTime.now().minute
// Instance of Alarm Manager
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
alarmTime.toLong(),
pendingIntent
)
There is a broadcast receiver called Alarm Receiver
. Inside of it is an instance of a function that display a notification. Here is the code of each.
// Alarm Broadcast Receiver
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Display Notification Callback
displayNotification(context)
}
}
// Function That Displays Notification
fun displayNotification(context: Context) {
createNotificationChannel(context)
// Variable That Holds Notification Manager
val notificationManager = getSystemService(context, NotificationManager::class.java) as NotificationManager
// Variable That Holds Notification
val notification = NotificationCompat.Builder(context, "channel_id")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Alarm Manager Test")
.setContentText("It works!")
.build()
// Instance of Notification Manager
notificationManager.notify(1, notification)
}
// Function That Creates Notification Channel
fun createNotificationChannel(context: Context) {
// Variable That Holds Notification Manager
val notificationManager = getSystemService(context, NotificationManager::class.java) as NotificationManager
// Variable That Holds Notification Channel
val notificationChannel = NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_LOW)
// Instance of Notification Manager
notificationManager.createNotificationChannel(notificationChannel)
}
The final app with include the new TimePicker
composable. I have not included it into this test app yet. I just want to see if I can hard code a time into the alarm manager.
I thought about finding the difference in minutes between the future alarm time and the LocalDateTime.now
and subtracting them but I’m not quite sure how to do that, or at least in the context of selecting multiple different formatted times using the TimerPicker
composable.
I tried formatting the LocalDateTime.now
into a Long
and putting it into the triggerAtMillis
parameter but the app crashed when I launched it.
Those are the only solutions I could think of of.