Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android的鬧鐘

Android的鬧鐘

編輯:高級開發

鬧鐘的使用

  第一步:注冊鬧鐘(AlarmManager,PendingIntent,Intent,鬧鐘的時間)

  第二步:注冊鬧鐘廣播接受者(BroadcastReceiver)

  第三步:處理鬧鐘的時間(比如:打開一個應用,提示用戶時間到了 etc....)

  =========AlarmAlert.Java==========

  private void updateLayout() {

  。。。。。。。。。。

  。。。。。。。。。。

  /* snooze behavior: pop a snooze confirmation vIEw, kick alarm

  manager. */

  Button snooze = (Button) findVIEwById(R.id.snooze);

  snooze.requestFocus();

  snooze.setOnClickListener(new Button.OnClickListener() {

  public void onClick(VIEw v) {

  snooze();

  }

  });

  ........

  ........

  }

  // Attempt to snooze this alert.

  private void snooze() {

  final String snooze =

  PreferenceManager.getDefaultSharedPreferences(this)

  .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);

  int snoozeMinutes = Integer.parseInt(snooze);

  final long snoozeTime = System.currentTimeMillis()

  + (1000 * 60 * snoozeMinutes); //計算睡眠時間

  Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarm.id, snoozeTime);//保存睡眠的時間

  // Get the display time for the snooze and update the notification.

  final Calendar c = Calendar.getInstance();

  c.setTimeInMillis(snoozeTime);

  。。。。

  。。。。

  。。。。

  // Notify the user that the alarm has been snoozed.//通知給廣播接收者 AlarmReceiver.class 並處理鬧鐘時間

  Intent cancelSnooze = new Intent(this, AlarmReceiver.class);

  cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);

  cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);

  PendingIntent broadcast =

  PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);

  接上頁

  NotificationManager nm = getNotificationManager();

  。。。。。

  。。。。。。。。。。。。。

  stopService(new Intent(Alarms.ALARM_ALERT_ACTION));

  finish();

  }

  =========Alarm.Java==========

  static void saveSnoozeAlert(final Context context, final int id,

  final long time) {

  SharedPreferences prefs = context.getSharedPreferences(

  AlarmClock.PREFERENCES, 0);

  SharedPreferences.Editor ed = prefs.edit();

  if (id == -1) {

  clearSnoozePreference(ed);

  } else {

  ed.putInt(PREF_SNOOZE_ID, id);

  ed.putLong(PREF_SNOOZE_TIME, time); //保存提示的時間

  ed.commit();

  }

  // Set the next alert after updating the snooze.

  setNextAlert(context);

  }

  /**

  * If there is a snooze set, enable it in AlarmManager

  * @return true if snooze is set

  */

  private static boolean enableSnoozeAlert(final Context context) {

  SharedPreferences prefs = context.getSharedPreferences(

  AlarmClock.PREFERENCES, 0);

  int id = prefs.getInt(PREF_SNOOZE_ID, -1);

  if (id == -1) {

  return false;

  }

  long time = prefs.getLong(PREF_SNOOZE_TIME, -1); //取出設置鬧鐘的時間

  // Get the alarm from the db.

  final Alarm alarm = getAlarm(context.getContentResolver(), id);

  // The time in the database is either 0 (repeating) or a specific time

  // for a non-repeating alarm. Update this value so the AlarmReceiver

  // has the right time to compare.

  alarm.time = time; //將時間放入數據庫

  enableAlert(context, alarm, time);

  return true;

  }

  /**

  * Sets alert in AlarmManger and StatusBar. This is what will

  * actually launch the alert when the alarm triggers.

  接上頁

  *

  * @param alarm Alarm.

  * @param atTimeInMillis milliseconds since epoch

  */

  private static void enableAlert(Context context, final Alarm alarm,

  final long atTimeInMillis) {

  AlarmManager am = (AlarmManager)

  context.getSystemService(Context.ALARM_SERVICE);

  Intent intent = new Intent(ALARM_ALERT_ACTION);

  PendingIntent sender = PendingIntent.getBroadcast(

  context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //接受廣播的鬧鐘的廣播意圖 Action:“ALARM_ALERT_ACTION”

  am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); //設置鬧鐘

  /**

  //鬧鐘的廣播接受者

  * Glue class: connects AlarmAlert IntentReceiver to AlarmAlert

  * activity. Passes through Alarm ID.

  */

  public class AlarmReceiver extends BroadcastReceiver {

  .......

  .....

  .......

  /* launch UI, explicitly stating that this is not due to user action

  * so that the current app's notification management is not disturbed */

  Intent alarmAlert = new Intent(context, c);

  alarmAlert.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);

  alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK

  | Intent.FLAG_ACTIVITY_NO_USER_ACTION);

  context.startActivity(alarmAlert);

  ...

  ........

  // Play the alarm alert and vibrate the device.

  Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);

  playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);

  context.startService(playAlarm);

  ...........

  .....

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved