android Alarm 闹钟

AlarmManager

1.主要功能是在指定的时间执行指定的任务,要注意所有的定时任务在手机重启后会消失,如果需要重启后继续用,可以加个开机自启,然后重新设置.
2.用法:设置AlarmManager在指定的时间发送广播,在接收器中写任务
例子:
定时广播
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent it=new Intent(context,MyRecevier.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
写接收器类,并注册
<receiver android:name=".MyRecevier" android:process=".myreceiver"/>
<!--android:process 接收器进程名字,可任意填,不填默认为包名,在网上有网友发表:"真机测试,如果不填,在AlarmManager时间设为过去的时间时,会不停收到广播(死循环)"-->
3.方法以及常量介绍
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
设置广播什么时候发出,第一个参数表示闹钟的类型,可以为:
ELAPSED_REALTIME  当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3    (0x00000003)。   
ELAPSED_REALTIME_WAKEUP 能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。   
RTC  当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。   
RTC_WAKEUP 能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。 
前两者使用相对时间,时间从系统启动开始算起,SystemClock.elapsedRealtime()可以获得当前的相对时间,单位为毫秒,例如:alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender);  
后两者使用绝对时间,时间以1970.1.1号为参考时间,System.currentTimeMillis()获取从1970.1.1号以来的时间,单位为毫秒,例如:alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender);  


am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 10000, pi);
 在上面方法的基础上,添加了重复执行的功能,倒数第二参数为间隔时间,单位为毫秒
 
 am.cancel(pi);//取消已经注册的与参数匹配的闹铃
 public void cancel (PendingIntent operation) 
Since: API Level 1 Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.
 
void  setTimeZone(String timeZone)   //设置时区  Requires the permission android.permission.SET_TIME_ZONE.
void  setTime(long millis) //Set the system wall clock time. Requires the permission android.permission.SET_TIME.
 
PendingIntent Intent的进一步封装,添加了延迟执行功能。两者主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的.还有PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于context才可以存在。Intent对象里包含了要执行的操作所需要的信息,PendingIntent对象里还包含了要执行什么操作(发出广播,启动界面,...)
三种不同方式来得到PendingIntent实例。 
getBroadcast——通过该函数获得的PendingIntent将会扮演一个广播者的功能,就像调用 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让系统自动找到该行为处理对象。 
Java代码 
Intent intent = new Intent(AlarmController.this, MyReceiver.class);   
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);  

getActivity——通过该函数获得的PendingIntent可以直接启动新的activity, 就像调用 Context.startActivity(Intent)一样.不过值得注意的是要想这个新的Activity不再是当前进程存在的Activity 时。我们在intent中必须使用Intent.FLAG_ACTIVITY_NEW_TASK. 
Java代码 
// The PendingIntent to launch our activity if the user selects this notification   
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  new Intent(this, AlarmService.class), 0);  


getService——通过该函数获得的PengdingIntent可以直接启动新的Service,就像调用Context.startService()一样。 

// Create an IntentSender that will launch our service, to be scheduled  with the alarm manager.

mAlarmSender = PendingIntent.getService(AlarmService.this , 0 ,  new  Intent(AlarmService.this , AlarmService_Service. class ),  0 );  

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值