Android笔记:定时提醒、闹钟实现

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

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

两个核心的方法 :ide

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

this

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


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

 

咱们看到这里具体的实现都是基于IAlarmManager的,而IAlarmManager是一个aidl(Android Interface definition language),具体的就不详细介绍了,你们有兴趣的能够本身研究一下。.net

 

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

// 进行闹铃注册
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 的源码:xml

/**
 * 
 * @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中须要加入:blog

<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




转自:http://jakend.iteye.com/blog/1980842

相关文章
相关标签/搜索