wuqiong 2011-03-15
闹钟的使用
第一步:注册闹钟(AlarmManager,PendingIntent,Intent,闹钟的时间)
第二步:注册闹钟广播接受者(BroadcastReceiver)
第三步:处理闹钟的时间(比如:打开一个应用,提示用户时间到了 etc....)=========AlarmAlert.java========== private void updateLayout() {
。。。。。。。。。。
。。。。。。。。。。
/*snoozebehavior:popasnoozeconfirmationview,kickalarm
manager.*/
Buttonsnooze=(Button)findViewById(R.id.snooze);
snooze.requestFocus();
snooze.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
snooze();
}
});........
........
}// Attempt to snooze this alert.
privatevoidsnooze(){
finalStringsnooze=
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.
finalCalendarc=Calendar.getInstance();
c.setTimeInMillis(snoozeTime);。。。。
。。。。
。。。。//Notifytheuserthatthealarmhasbeensnoozed.//通知给广播接收者AlarmReceiver.class并处理闹钟时间
IntentcancelSnooze=newIntent(this,AlarmReceiver.class);
cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
cancelSnooze.putExtra(Alarms.ALARM_ID,mAlarm.id);
PendingIntentbroadcast=
PendingIntent.getBroadcast(this,mAlarm.id,cancelSnooze,0);
NotificationManager nm = getNotificationManager();。。。。。
。。。。。。。。。。。。。
stopService(newIntent(Alarms.ALARM_ALERT_ACTION));
finish();
}=========Alarm.java==========
staticvoidsaveSnoozeAlert(finalContextcontext,finalintid,
finallongtime){
SharedPreferencesprefs=context.getSharedPreferences(
AlarmClock.PREFERENCES,0);
SharedPreferences.Editored=prefs.edit();
if(id==-1){
clearSnoozePreference(ed);
}else{
ed.putInt(PREF_SNOOZE_ID,id);
ed.putLong(PREF_SNOOZE_TIME,time);//保存提示的时间
ed.commit();
}
//Setthenextalertafterupdatingthesnooze.
setNextAlert(context);
}/**
*Ifthereisasnoozeset,enableitinAlarmManager
*@returntrueifsnoozeisset
*/
privatestaticbooleanenableSnoozeAlert(finalContextcontext){
SharedPreferencesprefs=context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);int id = prefs.getInt(PREF_SNOOZE_ID, -1);
if(id==-1){
returnfalse;
}
long time = prefs.getLong(PREF_SNOOZE_TIME, -1); //取出设置闹钟的时间// Get the alarm from the db.
finalAlarmalarm=getAlarm(context.getContentResolver(),id);
//Thetimeinthedatabaseiseither0(repeating)oraspecifictime
//foranon-repeatingalarm.UpdatethisvaluesotheAlarmReceiver
//hastherighttimetocompare.
alarm.time = time; //将时间放入数据库中enableAlert(context, alarm, time);
returntrue;
}/**
*SetsalertinAlarmMangerandStatusBar.Thisiswhatwill
*actuallylaunchthealertwhenthealarmtriggers.
*
*@paramalarmAlarm.
*@paramatTimeInMillismillisecondssinceepoch
*/
privatestaticvoidenableAlert(Contextcontext,finalAlarmalarm,
finallongatTimeInMillis){
AlarmManageram=(AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntentsender=PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //接受广播的闹钟的广播意图 Action:“ALARM_ALERT_ACTION”am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); //设置闹钟
/**
//闹钟的广播接受者
*Glueclass:connectsAlarmAlertIntentReceivertoAlarmAlert
*activity.PassesthroughAlarmID.
*/
public class AlarmReceiver extends BroadcastReceiver {.......
.....
.......
/*launchUI,explicitlystatingthatthisisnotduetouseraction
*sothatthecurrentapp'snotificationmanagementisnotdisturbed*/
IntentalarmAlert=newIntent(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);...
........
//Playthealarmalertandvibratethedevice.
IntentplayAlarm=newIntent(Alarms.ALARM_ALERT_ACTION);
playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA,alarm);
context.startService(playAlarm);...........
.....
}