Android Alarm What is the difference between four types of Alarm that AlarmManager provides and when to use what?

后端 未结 4 1346
我在风中等你
我在风中等你 2021-01-11 15:50

I want to know the difference between RTC, RTC_WAKEUP, ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP.
I want to write an alarm application where I will set

相关标签:
4条回答
  • 2021-01-11 16:12

    There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.

    Source: https://developer.android.com/training/scheduling/alarms.html

    0 讨论(0)
  • 2021-01-11 16:14

    From the site you can get the difference between the 4 constanst Below is example of the setting alarm

    Calendar mCalendar = Calendar.getInstance();
            mCalendar.add(Calendar.SECOND, 20);
            Intent intent_Timer = new Intent(TimerEvents.this, AlarmReceiver.class);
            intent_Timer.putExtra("alarm_message", "Drax Rules!!!");
            // In reality, you would want to have a static variable for the request
            // code instead of 192837
            PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
                    intent_Timer, PendingIntent.FLAG_UPDATE_CURRENT);
            // Get the AlarmManager service
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), sender);
    

    Hope this will be helpful to you

    0 讨论(0)
  • 2021-01-11 16:22

    Types of Alarms :

    • ELAPSED_REALTIME – Fires the pending intent after the specified length of time since device boot. If the device is asleep, it fires when the device is next awake.
    • ELAPSED_REALTIME_WAKEUP – Fires the pending intent after the specified length of time since device boot. It wakes up the device if it is asleep.
    • RTC – Fires the pending intent at a specified time. If the device is asleep, it will not be delivered until the next time the device wakes up.
    • RTC_WAKEUP – Fires the pending intent at a specified time, waking up the device if asleep.
    0 讨论(0)
  • 2021-01-11 16:32

    ELAPSED_REALTIME

    Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

    ELAPSED_REALTIME_WAKEUP

    Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

    RTC

    Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

    RTC_WAKEUP

    Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

    0 讨论(0)
提交回复
热议问题