@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(TimerMgr.ALARM_SNOOZE_ACTION)) { snooze(); } else if (action.equals(TimerMgr.ALARM_DISMISS_ACTION)) { dismiss(false); } else { // ALARM_INTENT_EXTRA : 传递过来一个alarm Timer timer = intent.getParcelableExtra(TimerMgr.ALARM_INTENT_EXTRA); if (timer != null && mTimer.getTimerDef().getID() == timer.getTimerDef().getID()) { dismiss(true); } } }
// Dismiss the alarm. // 当闹铃响时,用户按下解除按钮时,调用此方法 private void dismiss(boolean killed) { // The service told us that the alarm has been killed, do not modify // the notification or stop the service. if (!killed) { // Cancel the notification and stop playing the alarm // 取消通知栏消息 NotificationManager nm = getNotificationManager(); nm.cancel(mTimer.getTimerDef().getID()); stopService(new Intent(TimerMgr.ALARM_ALERT_ACTION)); // 如果设置了间隔提醒,则不能停止闹铃 if (mTimer.getTimerDef().getMaxCount() == 1) { TimerMgr.disableSnoozeTimer(this, mTimer); } } finish(); }
@Override protected void onResume() { super.onResume(); // If the alarm was deleted at some point, disable snooze. // 如果闹铃被删除了,则禁用贪睡 if (TimerMgr.getTimer(mTimer.getTimerDef().getID()) == null) { Button snooze = (Button) findViewById(R.id.snooze); snooze.setEnabled(false); } }
private void updateLayout() { LayoutInflater inflater = LayoutInflater.from(this); setContentView(inflater.inflate(R.layout.alarm_alert, null)); /** snooze behavior: pop a snooze confirmation view, kick alarm manager. */ // 打盹按钮,一定时间之后再响 Button snooze = (Button) findViewById(R.id.snooze); if (mTimer.getTimerDef().getMaxCount() == -1 || mTimer.getTimerDef().getMaxCount() > 1) { // 如果是循环闹钟,则不启用贪睡功能 snooze.setEnabled(false); } else { snooze.requestFocus(); } snooze.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { Util.log("click snooze"); snooze(); } }); /** dismiss button: close notification */ // 解除按钮,关闭闹钟 findViewById(R.id.dismiss) .setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { Util.log("click dismiss"); dismiss(false); } }); /** Set the title from the passed in alarm */ setTitle(); }
// Attempt to snooze this alert. // 当闹铃响时,用户按下贪睡按钮时,调用此方法 private void snooze() { // Do not snooze if the snooze button is disabled. // 打盹按钮被禁用,则直接返回 if (!findViewById(R.id.snooze).isEnabled()) { dismiss(false); return; } // 获取打盹时长:默认为10分钟 final String snooze = PreferenceManager.getDefaultSharedPreferences(this) .getString(ConfigurationActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE); int snoozeMinutes = Integer.parseInt(snooze); // 下一次闹铃时间 final long snoozeTime = System.currentTimeMillis() + (1000 * 60 * snoozeMinutes); TimerMgr.saveSnoozeTimer(AlarmAlertFullScreen.this, mTimer, snoozeMinutes); // Get the display time for the snooze and update the notification. final Calendar c = Calendar.getInstance(); c.setTimeInMillis(snoozeTime); // Append (snoozed) to the label. // 在label后面增加:(snoozed) String label = mTimer.getName(); label = getString(R.string.alarm_notify_snooze_label, label); // Notify the user that the alarm has been snoozed. // 用于下面的通知栏,当用户点击通知栏通知时,则取消打盹 Intent cancelSnooze = new Intent(this, AlarmReceiver.class); cancelSnooze.setAction(TimerMgr.CANCEL_SNOOZE); cancelSnooze.putExtra(TimerMgr.ALARM_ID, mTimer.getID()); // 获取一个PendingIntent,即将mAlarm.id广播给cancelSnooze PendingIntent broadcast = PendingIntent.getBroadcast(this, mTimer.getID(), cancelSnooze, 0); NotificationManager nm = getNotificationManager(); Notification n = new Notification(R.drawable.stat_notify_alarm, label, 0); // 在此处设置在nority列表里的该norifycation得显示情况。点击后则执行broadcast取消打盹 n.setLatestEventInfo( this, label, getString( R.string.alarm_notify_snooze_text, // Alarm set for %s. Select to cancel. Util.formatTime(c)), broadcast); // FLAG_AUTO_CANCEL 用户点击后自动取消 // FLAG_ONGOING_EVENT 将此通知放到通知栏的"Ongoing"即"正在运行"组中 n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT; nm.notify(mTimer.getTimerDef().getID(), n); // Snoozing for %d minutes. String displayTime = getString(R.string.alarm_alert_snooze_set, snoozeMinutes); // Intentionally log the snooze time for debugging. // Display the snooze minutes in a toast. Toast.makeText(AlarmAlertFullScreen.this, displayTime, Toast.LENGTH_LONG).show(); // 停止AlarmAlert服务 stopService(new Intent(TimerMgr.ALARM_ALERT_ACTION)); finish(); }
private void setTitle() { String label = mTimer.getTimerDef().getName(); TextView title = (TextView) findViewById(R.id.alertTitle); title.setText(label); }