Android原生模块DeskClock分析1(6)
DeskClock分析文档
? AlarmReceiver类继承BroadcastReceiver,该类重写onReceive()方法,定义
了NotificationManager()和updateNotification()。 ?
onReceive()方法接受系统发来的广播
@Override
public void onReceive(Context context, Intent intent) { //杀死闹铃
if (Alarms.ALARM_KILLED.equals(intent.getAction())) {Log.e(\
// The alarm has been killed, update the notification updateNotification(context, (Alarm) //手动杀死
intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA),
//超时杀死
intent.getIntExtra(Alarms.ALARM_KILLED_TIMEOUT, -1)); return;
} else if (Alarms.CANCEL_SNOOZE.equals(intent.getAction())) { Log.e(\
Alarms.saveSnoozeAlert(context, -1, -1); return; }
Alarm alarm = null; //
final byte[] data =
intent.getByteArrayExtra(Alarms.ALARM_RAW_DATA); if (data != null) {Log.e(\ Parcel in = Parcel.obtain();
in.unmarshall(data, 0, data.length); in.setDataPosition(0);
alarm = Alarm.CREATOR.createFromParcel(in); }
if (alarm == null) {Log.e(\
Log.v(\intent\
return; }
// Intentionally verbose: always log the alarm time to provide useful
// information in bug reports.
16
DeskClock分析文档
long now = System.currentTimeMillis(); SimpleDateFormat format =
new SimpleDateFormat(\
Log.v(\ + format.format(new Date(alarm.time)));
if (now > alarm.time + STALE_WINDOW * 1000) {Log.e(\ if (Log.LOGV) {
Log.v(\ } return; }
// 保证cpu唤醒前,通过AlarmAlertWakeLock和AlarmAlert将 AlarmAlertWakeLock.acquireCpuWakeLock(context);
/* Close dialogs and window shade */ Intent closeDialogs = new
Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); context.sendBroadcast(closeDialogs);
//决定哪些活动开始基于keyguard的状态. Class c = AlarmAlert.class;
KeyguardManager km = (KeyguardManager) context.getSystemService( Context.KEYGUARD_SERVICE);Log.e(\ if (km.inKeyguardRestrictedInputMode()) {Log.e(\
// 使用全屏活动以保证安全.
c = AlarmAlertFullScreen.class; }
/* 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);
// 如果snooze,禁用.
Alarms.disableSnoozeAlert(context, alarm.id); // 如果没有重复,禁用
17
DeskClock分析文档
if (!alarm.daysOfWeek.isRepeatSet()) {Log.e(\ Alarms.enableAlarm(context, alarm.id, false); } else {Log.e(\
//如果有下一个警报。enableAlarm通知setNextAler,避免调用它的两次. Alarms.setNextAlert(context); }
// 启用闹铃和震动装置.
Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION); playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm); context.startService(playAlarm);
Intent notify = new Intent(context, AlarmAlert.class); notify.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
PendingIntent pendingNotify = PendingIntent.getActivity(context, alarm.id, notify, 0);
String label = alarm.getLabelOrDefault(context); Log.e(\
Notification n = new Notification(R.drawable.stat_notify_alarm, label, alarm.time);
n.setLatestEventInfo(context, label,
context.getString(R.string.alarm_notify_text), pendingNotify);
n.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONGOING_EVENT; n.defaults |= Notification.DEFAULT_LIGHTS;
NotificationManager nm = getNotificationManager(context); nm.notify(alarm.id, n);
}
? updateNotification()方法更新消息 private void updateNotification(Context context, Alarm alarm, int timeout) {
NotificationManager nm = getNotificationManager(context);
// If the alarm is null, just cancel the notification. if (alarm == null) {
if (Log.LOGV) {Log.e(\
18
DeskClock分析文档
Log.v(\ } return; }
// 当点击,发出SetAlarm.
Intent viewAlarm = new Intent(context, SetAlarm.class); viewAlarm.putExtra(Alarms.ALARM_ID, alarm.id); PendingIntent intent =
PendingIntent.getActivity(context, alarm.id, viewAlarm, 0);
//更新通知显示警报已经消失.
String label = alarm.getLabelOrDefault(context); Log.e(\
Notification n = new Notification(R.drawable.stat_notify_alarm, label, alarm.time);
n.setLatestEventInfo(context, label,
context.getString(R.string.alarm_alert_alert_silenced, timeout),
intent);
n.flags |= Notification.FLAG_A …… 此处隐藏:2540字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




