Android笔记:定时提醒、闹钟实现
android要实现定时的功能那肯定就要用到闹铃相关的技术, 那么android闹铃实现是基于 AlarmManager 这个类的,首先我们来看一下它的几个主要的方法。 两个核心的方法 : 1 private final IAlarmManagermService; 1 2 3 4 5 6 public void set( int type, long triggerAtMillis,PendingIntentoperation){ try { mService.set(type,triggerAtMillis,operation); } catch (RemoteExceptionex){ } } 和 1 2 3 4 5 6 7 public void setRepeating( int type, long triggerAtMillis, long intervalMillis,PendingIntentoperation){ try { mService.setRepeating(type,triggerAtMillis,intervalMillis,operation); } catch (RemoteExceptionex){ } } 第一个方法主要功能是注册一个比较简单的闹铃,第二个方法是注册一个重复的闹铃,这里重复的意思是指:设置5s, 那么每隔5s会执行一次 。 我们看到这里具体的实现都是基于IAlarmManager的,而IAlarmManager是一个aidl(Android Interface definition language),具体的就不详细介绍了,大家有兴趣的可以自己研究一下。 下面我来看下set(int type, long triggerAtMillis, PendingIntent operation)方法是如何调用的: 1 2 3 4 5 6 7 8 9 10 11 //进行闹铃注册 Intentintent= new Intent(MainActivity. this ,AlarmReceiver. class ); PendingIntentsender=PendingIntent.getBroadcast(MainActivity. this , 0 ,intent, 0 ); //过10s执行这个闹铃 Calendarcalendar=Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 10 ); AlarmManagermanager=(AlarmManager)getSystemService(ALARM_SERVICE); manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender); 看一下AlarmReceiver 的源码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /** * *@ClassName:AlarmReceiver *@Description:闹铃时间到了会进入这个广播,这个时候可以做一些该做的业务。 *@authorHuHood *@date2013-11-25下午4:44:30 * */ public class AlarmReceiver extends BroadcastReceiver{ @Override public void onReceive(Contextcontext,Intentintent){ Toast.makeText(context, "闹铃响了,可以做点事情了~~" ,Toast.LENGTH_LONG).show(); } } 还有别忘了,AndroidManifest.xml中需要加入: 1 <receiverandroid:name= "com.example.alarmmanagerdemo.AlarmReceiver" android:process= ":remote" > 运行之后,过10s 弹出 "闹铃响了,可以做点事情了~~", 说明成功了。 ok,这个结果肯定不是我们想要的,我们想要的功能是每天定时提醒的功能,那么需要基于 setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation) 这个方法来实现,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Intentintent= new Intent(MainActivity. this ,AlarmReceiver. class ); PendingIntentsender=PendingIntent.getBroadcast(MainActivity. this , 0 ,intent, 0 ); long firstTime=SystemClock.elapsedRealtime(); //开机之后到现在的运行时间(包括睡眠时间) long systemTime=System.currentTimeMillis(); Calendarcalendar=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; //进行闹铃注册 AlarmManagermanager=(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 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1679698,如需转载请自行联系原作者