自己的alarm学习笔记
alarm的主要工作流程分为以下三步:
1.APP获取服务
2.AlarmService和AlarmManger交互
3.Alarm通过广播,或activity,service通知APP
1.应用分析:
接下来可以写一个安卓测试demo来测试:
AlarmManager alarmmanager = (AlarmManager)getSystemService(ALARM_SERVICE); //第一步,获取服务,当然也就包含了第二步(AlarmService和AlarmManger交互,那是系统的事情了)
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, ActionBroadCast.class), Intent.FLAG_ACTIVITY_NEW_TASK); //绑定广播</span>
long now = System.currentTimeMillis();
setInexactRepeating(AlarmManager.RTC_WAKEUP, now, 3000, pi);//参考这里http://www.android-doc.com/reference/android/app/AlarmManager.html
@Override
public void onReceive(Context context, Intent intent) { <span style="font-family: Arial, Helvetica, sans-serif;">//第三步,注册广播,等待系统通知</span>
// TODO Auto-generated method stub
Log.e("ActionBroadCast", "New Message !" + num++);
}
intervalMillisinterval in milliseconds between subsequent repeats of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY then the alarm will be phase-aligned with other alarms to reduce the number of wakeups. Otherwise, the alarm will be set as though the application had called setRepeating(int, long, long, PendingIntent)
. As of API 19, all repeating alarms will be inexact and subject to batching with other alarms regardless of their stated repeat interval.<span style="color: rgb(34, 34, 34); font-family: Roboto, sans-serif; background-color: rgb(249, 249, 249);"><em><span style="color: rgb(34, 34, 34); font-family: Roboto, sans-serif; background-color: rgb(249, 249, 249);"><em><span style="font-size:14px;">operation <span style="color: rgb(34, 34, 34); font-family: Roboto, sans-serif; background-color: rgb(249, 249, 249);">Action to perform when the alarm goes off; typically comes from </span><code style="color: rgb(0, 102, 0); font-stretch: normal; line-height: 14px; margin-bottom: 0px; background-color: rgb(249, 249, 249);"><a target=_blank href="http://www.android-doc.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int)" style="color: rgb(37, 138, 175); text-decoration: none; margin-bottom: 0px;">IntentSender.getBroadcast()</a></code><span style="color: rgb(34, 34, 34); font-family: Roboto, sans-serif; background-color: rgb(249, 249, 249);">.</span></span></em></span></em></span>
2.框架分析:
Alarm在安卓系统中主要分为如下几层:
框架中的主要调用流程如下: