How Do You Use A Formatted Time To Trigger An Alarm Using Alarm Manager In Android?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật