📄 alarmmanager.java
字号:
* @param dayOfMonth day of month of the alarm. Allowed values 1-7 * (1 = Sunday, 2 = Monday, ...), or -1 for all. * <code>java.util.Calendar</code> constants can be used. * @param month month of the alarm. Allowed values 0-11 (0 = January, * 1 = February, ...), or -1 for all. <code>java.util.Calendar</code> * constants can be used. * @param dayOfWeek day of week of the alarm. Allowed values 1-31, * or -1 for all. * @param year year of the alarm. When this field is not set * (i.e. -1) the alarm is repetitive (i.e. it is rescheduled when * reached). * @param listener the alarm listener. * @return the AlarmEntry. * @exception PastDateException if the alarm date is in the past * (or less than 1 second away from the current date). */ public synchronized AlarmEntry addAlarm(String _name, int _minute, int _hour, int _dayOfMonth, int _month, int _dayOfWeek, int _year, AlarmListener _listener) throws PastDateException { AlarmEntry entry = new AlarmEntry(_name, _minute, _hour, _dayOfMonth, _month, _dayOfWeek, _year, _listener); addAlarm(entry); return entry; } /** @deprecated for backwards compatibility, w/o name param: */ public AlarmEntry addAlarm(int _minute, int _hour, int _dayOfMonth, int _month, int _dayOfWeek, int _year, AlarmListener _listener) throws PastDateException { return addAlarm(_minute, _hour, _dayOfMonth, _month, _dayOfWeek, _year,_listener); } /** * Adds an alarm for a specified date or matching dates (for unrestricted * fields). * * @param minutes minutes of the alarm. Allowed values 0-59, or -1 for all. * @param hours hours of the alarm. Allowed values 0-23, or -1 for all. * @param daysOfMonth days of month of the alarm. Allowed values 1-7 * (1 = Sunday, 2 = Monday, ...), or -1 for all. * <code>java.util.Calendar</code> constants can be used. * @param months months of the alarm. Allowed values 0-11 (0 = January, * 1 = February, ...), or -1 for all. <code>java.util.Calendar</code> * constants can be used. * @param daysOfWeek days of week of the alarm. Allowed values 1-31, * or -1 for all. * @param year year of the alarm. When this field is not set * (i.e. -1) the alarm is repetitive (i.e. it is rescheduled when * reached). * @param listener the alarm listener. * @return the AlarmEntry. * @exception PastDateException if the alarm date is in the past * (or less than 1 second away from the current date). */ public synchronized AlarmEntry addAlarm(String _name, int[] _minutes, int[] _hours, int[] _daysOfMonth, int[] _months, int[] _daysOfWeek, int _year, AlarmListener _listener) throws PastDateException { AlarmEntry entry = new AlarmEntry(_name, _minutes, _hours, _daysOfMonth, _months, _daysOfWeek, _year, _listener); addAlarm(entry); return entry; } /** @deprecated for backwards compatibility, w/o name param: */ public AlarmEntry addAlarm(int[] _minutes, int[] _hours, int[] _daysOfMonth, int[] _months, int[] _daysOfWeek, int _year, AlarmListener _listener) throws PastDateException { return addAlarm( null, _minutes, _hours, _daysOfMonth, _months, _daysOfWeek,_year,_listener); } /** * Adds an alarm for a specified AlarmEntry * * @param entry the AlarmEntry. * @exception PastDateException if the alarm date is in the past * (or less than one second away from the current date). */ public synchronized void addAlarm(AlarmEntry _entry) throws PastDateException { debug("Add a new alarm entry : " + _entry); queue.add(_entry); if (queue.first().equals(_entry)) { debug("This new alarm is the top one, update the waiter thread"); waiter.update(_entry.alarmTime); } } /** * Removes the specified AlarmEntry. * * @param entry the AlarmEntry that needs to be removed. * @return <code>true</code> if there was an alarm for this date, * <code>false</code> otherwise. */ public synchronized boolean removeAlarm(AlarmEntry _entry) { boolean found = false; if( ! queue.isEmpty() ) { AlarmEntry was_first = (AlarmEntry)queue.first(); found = queue.remove(_entry); // update the queue if it's not now empty, and the first alarm has changed if ( !queue.isEmpty() && _entry.equals(was_first) ) { waiter.update( ((AlarmEntry) queue.first()).alarmTime ); } } return found; } // removeAlarm() /** * Removes all the alarms. No more alarms, even newly added ones, will * be fired. */ public synchronized void removeAllAlarms() { queue.clear(); } /** * Removes all the alarms. No more alarms, even newly added ones, will * be fired. */ public synchronized void removeAllAlarmsAndStop() { waiter.stop(); waiter = null; queue.clear(); } public boolean isStopped() { return (waiter == null); } /** Tests whether the supplied AlarmEntry is in the manager. @param AlarmEntry @return boolean whether AlarmEntry is contained within the manager */ public synchronized boolean containsAlarm(AlarmEntry _alarmEntry) { return queue.contains(_alarmEntry); } /** * Returns a copy of all alarms in the manager. */ public synchronized List getAllAlarms() { List result = new ArrayList(); Iterator iterator = queue.iterator(); while (iterator.hasNext()) { result.add(iterator.next()); } return result; } /** * This is method is called when an alarm date is reached. It * is only be called by the the AlarmWaiter or by itself (if * the next alarm is less than 1 second away). */ protected synchronized void ringNextAlarm() { debug("ringing next alarm"); // if the queue is empty, there's nothing to do if (queue.isEmpty()) { return; } // Removes this alarm and notifies the listener AlarmEntry entry = (AlarmEntry) queue.first(); queue.remove(entry); // NOTE: if the entry is still running when its next alarm time comes up, // that execution of the entry will be skipped. if( entry.isRingInNewThread() ) { new Thread( new RunnableRinger(entry) ).start(); } else { // ring in same thread, sequentially.. can delay other alarms try { entry.ringAlarm(); } catch(Exception e) { e.printStackTrace(); } } // Reactivates the alarm if it is repetitive if (entry.isRepeating) { entry.updateAlarmTime(); queue.add(entry); } // Notifies the AlarmWaiter thread for the next alarm if (queue.isEmpty()) { debug("no more alarms to handle; queue is empty"); } else { long alarmTime = ((AlarmEntry)queue.first()).alarmTime; if (alarmTime - System.currentTimeMillis() < 1000) { debug("next alarm is within 1 sec or already past - ring it without waiting"); ringNextAlarm(); } else { debug("updating the waiter for next alarm: " + queue.first()); waiter.restart(alarmTime); } } } // notifyListeners() /** * Stops the waiter thread before ending. */ public void finalize() { if (waiter != null) waiter.stop(); } /** * Used to ring an AlarmEntry in a new Thread. * @see com.jtheory.jdring.AlarmEntry#setRingInNewThread() */ private class RunnableRinger implements Runnable { AlarmEntry entry = null; RunnableRinger(AlarmEntry _entry) { entry = _entry; } public void run() { try { entry.ringAlarm(); } catch(Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -