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

江枫思渺然 提交于 2019-12-04 00:38:01

问题


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 alarm and close my application and expect for alarm for the time set.
There will be multiple alarms. Right now I am writing for emulator but later will test on device. In emulator, once I set the alarm and close the emulator and restart it, then will it be cleared, as I find with RTC, RTC_WAKEUP and ELAPSED_REALTIME. I am confused. Should I used ELAPSED_REALTIME_WAKEUP? I have not seen any tutorial using ELAPSED_REALTIME_WAKEUP. please explain. Thanks.


回答1:


You could read this :

http://developer.android.com/reference/android/app/AlarmManager.html

You have all the difference beetween alarms




回答2:


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.




回答3:


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.



回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/5102073/android-alarm-what-is-the-difference-between-four-types-of-alarm-that-alarmmanag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!