`

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);


...........

 

.....

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics