1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public class TimerManager {
public static final String TAG = "TimerManager"; public static final String ACTION = "com.lonbon.lonbonbroadcast.manager.TestReceiver"; private static final int TIME_INTERVAL = 1000 * 60 * 60 * 24;// 24h private Integer[] weeks; private int weeksIndex; private TimerManagerCallback timerManagerCallback; private static final String HOUR = "hour"; private static final String MINUTE = "minute"; private static final String ID = "id"; private AlarmManager alarmManager; private PendingIntent paddingIntent; private TestReceiver testReceiver; private Context mContext; private IntentFilter filter; private boolean stopFlag = false;
public void setAlarmManager(Context context, int hour, int minute) { Log.i(TAG, "setAlarmManager: "); mContext = context;
//设置时间 Calendar calendar = Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get (Calendar.DAY_OF_MONTH), hour, minute, 0);
testReceiver = new TestReceiver(); filter = new IntentFilter(); filter.addAction(ACTION + hour + minute); mContext.registerReceiver(testReceiver, filter);
alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ACTION + hour + minute); intent.putExtra(HOUR, hour + ""); intent.putExtra(MINUTE, minute + ""); intent.putExtra(ID, minute + ""); paddingIntent = PendingIntent.getBroadcast(context, hour + minute, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, calMethod(calendar.getTimeInMillis()), paddingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calMethod(calendar.getTimeInMillis()), paddingIntent); } else { alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calMethod(calendar.getTimeInMillis()), TIME_INTERVAL, paddingIntent); } stopFlag = false; }
/** * @param dateTime 传入的是时间戳(设置当天的年月日+从选择框拿来的时分秒) * @return 返回起始闹钟时间的时间戳 */ private long calMethod(long dateTime) { long time = 0; //weekflag == 0表示是按天为周期性的时间间隔或者是一次行的,weekfalg非0时表示每周几的闹钟并以周为时间间隔
Calendar c = Calendar.getInstance(); int week = c.get(Calendar.DAY_OF_WEEK); if (1 == week) { week = 7; } else if (2 == week) { week = 1; } else if (3 == week) { week = 2; } else if (4 == week) { week = 3; } else if (5 == week) { week = 4; } else if (6 == week) { week = 5; } else if (7 == week) { week = 6; } int weekflag = 0; if (weeks != null) { for (int i = 0; i < weeks.length; i++) { if (weeks[i] == null) { continue; } if (weeks[i] == week && dateTime > System.currentTimeMillis()) { weeksIndex = i; break; } if (weeks[i] > week) { weeksIndex = i; break; } else { weeksIndex = 0; } } if (weeksIndex < weeks.length) { if (weeks[weeksIndex] != null) { weekflag = weeks[weeksIndex]; } } }
if (weekflag == week) { if (dateTime > System.currentTimeMillis()) { time = dateTime; } else { time = dateTime + 7 * 24 * 3600 * 1000; } } else if (weekflag > week) { time = dateTime + (weekflag - week) * 24 * 3600 * 1000; } else if (weekflag < week) { time = dateTime + (weekflag - week + 7) * 24 * 3600 * 1000; } return time; }
public void setWeeks(String[] weeks) {
Integer[] a = new Integer[weeks.length - 1]; for (int i = 0; i < weeks.length; i++) { if (!weeks[i].isEmpty()) { a[i - 1] = Integer.valueOf(weeks[i]); } } this.weeks = a; }
public void cancel() { if (alarmManager != null && paddingIntent != null) { alarmManager.cancel(paddingIntent); } if (!stopFlag && mContext != null) { mContext.unregisterReceiver(testReceiver); stopFlag = true; } }
public interface TimerManagerCallback { void onReceive(TimerManager timerManager); }
public class TestReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if (timerManagerCallback != null) { timerManagerCallback.onReceive(TimerManager.this); } int hour = Integer.parseInt(intent.getStringExtra(HOUR)); int minute = Integer.parseInt(intent.getStringExtra(MINUTE)); Calendar calendar = Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get (Calendar.DAY_OF_MONTH), hour, minute, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, calMethod(calendar.getTimeInMillis()), paddingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calMethod(calendar.getTimeInMillis()), paddingIntent); } else { alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calMethod(calendar.getTimeInMillis()), TIME_INTERVAL, paddingIntent); }
} }
public void setTimerManagerCallback(TimerManagerCallback timerManagerCallback) { this.timerManagerCallback = timerManagerCallback; }
public TestReceiver getTestReceiver() { return testReceiver; } }
|