There is a TimePicker, the user has chosen a time, for example 13:15:00, now on his phone 12:13:00, at the time he chose a notification should appear.

How to implement it on android?

Additional question: Is such a thing better implemented using Backend?

    1 answer 1

    For this there is a class AlarmManager . Need to

    1. Calculate the time of the event
    2. Generate a PendingIntent that the operating system activates when an event occurs.
    3. Make a handler for this intent (most likely you will need a BroadcastReceiver registered in the application manifest)
    4. "Start" this "alarm clock".

      AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); // через 2 суток long delay = TimeUnit.HOURS.toMillis(48L); long time = System.currentTimeMillis() + delay; alarmManager.set(AlarmManager.RTC, time, pendingIntent); 

    What you should pay attention to:

    1. There are two types of clocks in the system: System.currentTimeMillis() is the number of milliseconds since the beginning of the era, and SystemClock.elapsedRealtime() is the number of milliseconds since the device was rebooted. If the time on the device is adjusted, the first will change the readings, the second - no.

    2. If the device is "asleep", the operating system will execute the event with a delay. If you need to wake the device at a certain moment, that is, the constants AlarmManager.RTC_WAKEUP and AlarmManager.ELAPSED_REALTIME_WAKEUP . But even this does not guarantee high accuracy of the event.

    3. To cancel the alarm, you need to pass alarmManager.cancel() with the same PendingIntent. Resetting the alarm with the same PendingIntent will change the time.

    4. The ability to add a listener to the AlarmMangager appeared only in API 24, it is early to use in applications in general.

    How to create PendingIntent or BroadcastReceiver, I assume that you know, in any case, this is a separate story.