闹钟的使用
第一步:注册闹钟(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. 【Android(安卓)开发教程】AnalogClock和DigitalClock
  2. Android初级教程IP拨号器初识广播接受者
  3. 屏蔽android ota升级包时间戳比较
  4. Android实现自定义时钟控件
  5. Android关于获取时间的记录(小结)
  6. Android时区列表+编译固件的时候设置系统默认时区
  7. Android(安卓)时间显示问题
  8. android anr traces日志分析方法
  9. 时间和日期选择器DatePicker和TimePicker的使用

随机推荐

  1. Android(安卓)View类属性及方法
  2. android 设置progressbar的背景颜色
  3. [置顶] android 抽屉效果,内容GridView来
  4. 深入Gradle插件开发
  5. Android(安卓)Things APP版本更新解决方
  6. android 中文 API (41) —— RatingBar.O
  7. Android(安卓)四大组件之 Service(一)
  8. android地图定位
  9. Android(安卓)android:allowBackup waiti
  10. Android—— 4.2 Vold挂载管理_主体构建m