android要实现定时的功能那肯定就要用到闹铃相关的技术,

那么android闹铃实现是基于 AlarmManager 这个类的,首先我们来看一下它的几个主要的方法。

打开AlarmManager的源码,如下图显示:

两个核心的方法 :

private final IAlarmManager mService;
public void set(int type, long triggerAtMillis, PendingIntent operation) {      try {                                                                           mService.set(type, triggerAtMillis, operation);                         } catch (RemoteException ex) {                                              }                                                                       }               

 public void setRepeating(int type, long triggerAtMillis,                                 long intervalMillis, PendingIntent operation) {                              try {                                                                                mService.setRepeating(type, triggerAtMillis, intervalMillis, operation);     } catch (RemoteException ex) {                                                   }                                                                            }                                                                               


第一个方法主要功能是注册一个比较简单的闹铃,第二个方法是注册一个重复的闹铃,这里重复的意思是指:设置5s, 那么每隔5s会执行一次 。

我们看到这里具体的实现都是基于IAlarmManager的,而IAlarmManager是一个aidl(Android Interface definition language),具体的就不详细介绍了,大家有兴趣的可以自己研究一下。

下面我来看下set(int type, long triggerAtMillis, PendingIntent operation)方法是如何调用的:

// 进行闹铃注册Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);// 过10s 执行这个闹铃Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.SECOND, 10);AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);



看一下AlarmReceiver 的源码:

/** *  * @ClassName: AlarmReceiver   * @Description: 闹铃时间到了会进入这个广播,这个时候可以做一些该做的业务。 * @author HuHood * @date 2013-11-25 下午4:44:30   * */public class AlarmReceiver extends BroadcastReceiver {@Override    public void onReceive(Context context, Intent intent) {Toast.makeText(context, "闹铃响了, 可以做点事情了~~", Toast.LENGTH_LONG).show();    }}

还有别忘了,AndroidManifest.xml中需要加入:

<receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" android:process=":remote">


运行之后,过10s 弹出 "闹铃响了,可以做点事情了~~", 说明成功了。

ok,这个结果肯定不是我们想要的,我们想要的功能是每天定时提醒的功能,那么需要基于

setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation)

这个方法来实现,代码如下:

Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);long firstTime = SystemClock.elapsedRealtime();// 开机之后到现在的运行时间(包括睡眠时间)long systemTime = System.currentTimeMillis();Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());// 这里时区需要设置一下,不然会有8个小时的时间差calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));calendar.set(Calendar.MINUTE, mMinute);calendar.set(Calendar.HOUR_OF_DAY, mHour);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);// 选择的定时时间long selectTime = calendar.getTimeInMillis();// 如果当前时间大于设置的时间,那么就从第二天的设定时间开始if(systemTime > selectTime) {Toast.makeText(MainActivity.this,"设置的时间小于当前时间", Toast.LENGTH_SHORT).show();calendar.add(Calendar.DAY_OF_MONTH, 1);selectTime = calendar.getTimeInMillis();}// 计算现在时间到设定时间的时间差long time = selectTime - systemTime;firstTime += time;// 进行闹铃注册AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,                firstTime, DAY, sender);Log.i(TAG,"time ==== " + time +", selectTime ===== "+ selectTime + ", systemTime ==== " + systemTime +", firstTime === " + firstTime);Toast.makeText(MainActivity.this,"设置重复闹铃成功! ", Toast.LENGTH_LONG).show();


以上的思路大致是这样的,首先根据设置的时间,算出当前时间离设置时间的时间差,加上这个时间差,我们就知道第一次提醒的时间,然后再设定重复的时间间隔,我们这里设置1天就可以了。



现在解释一下setRepeating中第一个参数:


AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠;当手机休眠时不发射闹钟。
AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠;
AlarmManager.ELAPSED_REALTIME,真实时间流逝闹钟,不唤醒手机休眠;当手机休眠时不发射闹钟。
AlarmManager.ELAPSED_REALTIME_WAKEUP,真实时间流逝闹钟,当闹钟发躰时唤醒手机休眠;

RTC闹钟和ELAPSED_REALTIME最大的差别就是前者可以通过修改手机时间触发闹钟事件,后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算。




以上摘自:http://hualang.iteye.com/blog/1304054


还有,我们这里是调用广播的形式来实现的,也可以用service的方式来进行实现,只要把PendingIntent改动一下即可,相关代码:
PendingIntent.getService(MainActivity.this, 0,new Intent(MainActivity.this,AlarmService.class), 0);
其他的均相同。


完整Demo下载:
http://download.csdn.net/detail/jakend/6612033

更多相关文章

  1. Android获取CPU使用率的几种方式
  2. android 每天定时提醒功能实现
  3. android的PreferenceActivity
  4. android SystemClock设置系统时间需要system权限
  5. android开发,你还在犹豫什么呢?进来看看花不了你很多时间的
  6. Android(安卓)时间滚动控件
  7. Android(安卓)性能分析工具 TraceView
  8. Android中实现日期时间选择器(DatePicker和TimePicker)
  9. 判断Android系统时间是否是24小时制

随机推荐

  1. android ksoap用法
  2. Android(安卓)中Intent的putExtra()方法
  3. android,总结,判断应用程序是否切换到后
  4. android 获取当前时间
  5. android C/C++ source files 全局宏定义
  6. Android 之 setContentView 源码阅读
  7. android 调用系统相机程序,存放文件夹创建
  8. Android聊天软件开发(基于网易云IM即时通
  9. Android requestFeature() must be calle
  10. android ping 命令使用问题