资源描述:
《android闹钟功能报告》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、ADROID2.1架构解析——闹钟分析透彻,有点基础都能看懂!1设置定时时间文件:packages/apps/alarmclock/src/com/android/alarmclock/Alarms.javaprivatestaticvoidenableAlert(Contextcontext,finalAlarmalarm,finallongatTimelnMillis){AlarmManageram=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);♦♦參am.set(Al
2、armManager.RTC_WAKEUP,atTimelnMillis,sender);}由闹钟管理器设置定时时间。2闹钟管理器2.1定时设置文件:frameworks/base/core/java/android/app/AlarmManager.javapublicstaticfinalintRTC_WAKEUP=0;publicstaticfinalintRTC=1;publicstaticfinalintELAPSED_REALT1ME_WAKEUP=2;publicstaticfinalintELAPSED_REALTIME=3;Alar
3、mManager(IAlarmManagerservice){mService=service;}publicvoidset(inttype,longtriggerAtTime,Pendinglntentoperation){try{mService.set(type,triggerAtTime,operation);}catch(RemoteExceptionex){}}将type,triggerAtTime,operation等参数转向闹钟管理器服务。3闹钟管理器服务文件:frameworks/base/services/java/com/and
4、roid/server/AlarmManagerService.java3.1定时设置publicAlarmManagerService(Contextcontext){mDescriptor=init();)publicvoidset(inttype,longtriggerAtTime,Pendinglntentoperation){setRepeating(type,triggerAtTime,0,operation);publicvoidsetRepeating(inttype,longtriggerAtTirne,longinterval,P
5、endinglntentoperation){if(operation==null){Log.w(TAQnset/setRepeatingignoredbecausethereisnointent’’);return;}synchronized(mLock){Alarmalarm=newAlarm();alarm.type=type;alarm.when=triggerAtTime;alarm.repeatlnterval=interval;alarm.operation=operation;//Removethisalarmifalreadysch
6、eduled.removeLocked(operation);if(localLOGV)Log.v(TAG,"set:"+alarm);intindex=addAlarmLocked(alarm);if(index==0){setLocked(alarm);}}}privatevoidsetLocked(Alarmalarm){if(mDescriptor!=-1){set(mDescriptor,alarm.type,(alarm.when*1000*1000));}else{Messagemsg=Message.obtain();msg.what
7、=ALARM_EVENT;mHandIer.removeMessages(ALARM_EVENT);mHandler.sendMessageAtTime(msg,alarm.when);}}Init,set为底层操作函数,$et(mDescriptor,alarm.type,(alarm.when*1000*1000));由底层来完成定时设置。3.2等待计时结束privateclassAlarmThreadextendsThreadpublicvoidrun()while(true)intresult=waitForAlarm(mDe$criptor
8、);if((result&RTC_WAKEUP_MASK)!=0)triggerAlarmsLocked(m