Android alarms are dependent on two components. One is Alarmmanager one is calendar.
First of all, Calendar,calendar is a very handy time-date management easy, it stores date-time information, and can be set and get in various positions.
Calendar usage can be divided into the following steps
1. Get the Calendar instance
Calendar Calendar = Calendar.getinstance ();
2. Set or get date + time
Calendar.set (calendar.hour_of_day,3); Calendar.set (Calendar.year,3); int minute = calendar.get (calendar.minute); int
It is important to note that the get month in Calendar is +1.
Anyway you get a date + time data through this, now you need to start setting the alarm with this data.
First, we can imagine that there must be an action when the alarm time is up. Through this action, the developer responds to the design behavior of the alarm (such as turning on the screen, annoying waking music, and so on). So we seem to need a intent. This intent can open the activity, open the service or send a broadcast. However, this is not enough. Because the developer doesn't really know when the intent will start, it's entirely up to the user. So we need a pendingintent to pack the intent. Let's say I set an alarm in a.activity and want to open b.activity when the alarm goes off. I can make the following settings:
Alarmmanager Alarmmanager = (Alarmmanager) Getsystemservice (alarm_service); New Intent (A.This, B.class); = Pendingintent.getactivity (A.ThisID , intent, 0); Alarmmanager.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), pendingintent);
The alarm type is rtc_wakeup, which means that the alarm will wake up the phone even when the phone is dormant. Calendar.gettimeinmillis () is the alarm time set by the user. Pendingintent is the wake-up action that the developer has designed for the alarm clock, in this case, open b.activity.
The ID in Pendingintent's settings is very worrying. Some tutorials set it directly to 0, which seems to work correctly.
Not actually ! by looking at the information, if you set the ID directly to 0, the alarm will be set only one, and the alarm set later will overwrite the alarm set earlier. Therefore, when designing alarms, developers must set an ID for each alarm to distinguish them, otherwise it will cause pendingintent coverage.
Above ~
About the alarm clock on Android