Programming Android
 All Modules Pages

The following code sets a lock screen messagge containing an alarm clock icon and the next alarm time as text message. If no alarm is set at all the lock screen message will be deleted.

private void RefreshNotificationForLockScreen(Context context, AlarmInfo alarmInfo)
{
// Id for the notification. Has to be unique only within this code
// to identify one of possibly several messages from this app
// to remove or refresh.
// As we have only a single notification, choose an arbitrary id:
int myNotificationId = 18;
if (alarmInfo.alarmTime.isEmpty()) // i.e. no alarm is set
{
// Remove a possibly existing notification
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.cancel(myNotificationId);
}
else // alarm is set
{
// Create or actualize notification
Notification notification = new Notification.Builder(context)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle(alarmInfo.alarmTime)
.setContentText("Nächster Alarm in " + alarmInfo.durationUntilAlarm)
.setSmallIcon(R.drawable.alarm_clock)
.setOngoing(true) // user cannot close notification
.setAutoCancel(false).build();
notification.visibility = NotificationCompat.VISIBILITY_PUBLIC;
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(myNotificationId, notification);
}
}