1、 . . . . ADROID 2.1 架构解析——闹钟 分析透彻,有点基础都能看懂! 1 设置定时时间 文件:packages/apps/alarmclock/src//android/alarmclock/Alarms.java private static void enableAlert(Context context, final Alarm alarm, final long atTimeInMillis) { AlarmM
2、anager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); ... am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); ... } 由闹钟管理器设置定时时间。 2 闹钟管理器 2.1 定时设置 文件:frameworks/base/core/java/android/app/AlarmManager.java
3、 public static final int RTC_WAKEUP = 0; public static final int RTC = 1; public static final int ELAPSED_REALTIME_WAKEUP = 2; public static final int ELAPSED_REALTIME = 3; AlarmManager(IAlarmManager service) { mService = service; } public void set(int type, lon
4、g triggerAtTime, PendingIntent operation) { try { mService.set(type, triggerAtTime, operation); } catch (RemoteException ex) { } } 将type, triggerAtTime, operation等参数转向闹钟管理器服务。 3 闹钟管理器服务 文件:frameworks/base/services/java//android/server/AlarmManagerSer
5、vice.java 3.1 定时设置 public AlarmManagerService(Context context) { mDescriptor = init(); ... } public void set(int type, long triggerAtTime, PendingIntent operation) { setRepeating(type, triggerAtTime, 0, operation); } public void setRepeating(int t
6、ype, long triggerAtTime, long interval, PendingIntent operation) { if (operation == null) { Log.w(TAG, "set/setRepeating ignored because there is no intent"); return; } synchronized (mLock) { Alarm alarm = new Alarm();
7、 alarm.type = type; alarm.when = triggerAtTime; alarm.repeatInterval = interval; alarm.operation = operation; // Remove this alarm if already scheduled. removeLocked(operation); if (localLOGV) Log.v(TAG, "
8、set: " + alarm); int index = addAlarmLocked(alarm); if (index == 0) { setLocked(alarm); } } } private void setLocked(Alarm alarm) { if (mDescriptor != -1) { set(mDescriptor, alarm.type, (ala
9、rm.when * 1000 * 1000)); } else { Message msg = Message.obtain(); msg.what = ALARM_EVENT; mHandler.removeMessages(ALARM_EVENT); mHandler.sendMessageAtTime(msg, alarm.when); } } Init,set 为底层操作函
10、数,set(mDescriptor, alarm.type, (alarm.when * 1000 * 1000)); 由底层来完成定时设置。 3.2等待计时完毕 private class AlarmThread extends Thread { public void run() { ... while (true) { int result = waitForAlarm(mDescriptor);
11、 ... if ((result & RTC_WAKEUP_MASK) != 0) triggerAlarmsLocked(mRtcWakeupAlarms, triggerList, nowRTC); if ((result & RTC_MASK) != 0) triggerAlarm
12、sLocked(mRtcAlarms, triggerList, nowRTC); if ((result & ELAPSED_REALTIME_WAKEUP_MASK) != 0) triggerAlarmsLocked(mElapsedRealtimeWakeupAlarms,triggerList, nowELAPSED); if ((result & ELAPSED_R
13、EALTIME_MASK) != 0) triggerAlarmsLocked(mElapsedRealtimeAlarms, triggerList, nowELAPSED); ... } } } 在服务里采用线程来处理计时完毕返回的信息,根据等待计时完毕返回的信息触发不同的操作。 4 硬件调用(JNI机制) 文件:frameworks/base/services/jni/_android_server_AlarmManagerSer
14、vice.cpp 4.1 定时设置 static jint android_server_AlarmManagerService_init(JNIEnv* env, jobject obj) { #if HAVE_ANDROID_OS return open("/dev/alarm", O_RDWR); #else return -1; #endif } static void android_server_AlarmManagerService_close(JNIEnv* env, jobject obj, jint fd) { #if H
15、AVE_ANDROID_OS close(fd); #endif } static void android_server_AlarmManagerService_set(JNIEnv* env, jobject obj, jint fd, jint type, jlong nanoseconds) { #if HAVE_ANDROID_OS struct timespec ts; ts.tv_sec = NANOSECONDS_TO_SECONDS(nanoseconds); ts.tv_nsec = nanoseconds -
16、SECONDS_TO_NANOSECONDS(ts.tv_sec); int result = ioctl(fd, ANDROID_ALARM_SET(type), &ts); if (result < 0) { LOGE("Unable to set alarm to %lld: %s\n", nanoseconds, strerror(errno)); } #endif } 以上函数的绑定如下: {"init", "()I", (void*)android_server_Alarm
17、ManagerService_init}, {"close", "(I)V", (void*)android_server_AlarmManagerService_close}, {"set", "(IIJ)V", (void*)android_server_AlarmManagerService_set}, 由上可知,通过ioctl接口,设置定时时间。 4.2 等待计时完毕 static jint android_server_AlarmManagerService_waitForAlarm(JNIEnv* env, jobject obj, jin
18、t fd) { #if HAVE_ANDROID_OS int result = 0; do { result = ioctl(fd, ANDROID_ALARM_WAIT); } while (result < 0 && errno == EINTR); if (result < 0) { LOGE("Unable to wait on alarm: %s\n", strerror(errno));
19、 return 0; } return result; #endif } 以上函数的绑定如下: {"waitForAlarm", "(I)I", (void*)android_server_AlarmManagerService_waitForAlarm}, 由上可知,代码阻塞方式等待计时完毕或由意外中断完毕。 4 杂项 4.1 闹钟类型 public static final int ELAPSED_REALTIME //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,
20、是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。 public static final int ELAPSED_REALTIME_WAKEUP //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。 public static final int RTC //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 Sy
21、stem.currentTimeMillis()获得。系统值是1 (0x00000001) 。 public static final int RTC_WAKEUP //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。 Public static final int POWER_OFF_WAKEUP //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4(0x00000004)。 4.2 底层实现 在IOCTL中ANDROID_ALARM_SET(0)设置闹钟; 在IOCTL中ANDROID_ALARM_WAIT: 等待闹钟时间,其中 rv = wait_event_interruptible(alarm_wait_queue, alarm_pending); 会阻塞,在## mins后alarm_timer_triggered函数中改变alarm_pending值为真,并唤醒等待队列。 wake_up(&alarm_wait_queue); 在ANDROID_ALARM_WAIT的最后释放alarm_rtc_wake_lock。 5 / 5






