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 );
分享到:
相关推荐
都是src文件,希望帮助大家,我做android时钟的时候找到的,官网文件
Android AlarmClock 闹钟源码,已调试完成,可以本地运行,和系统闹钟不存在冲突,android系统版本必须为2.2 。
该PPT由一个高频率唤醒导致Android设备功耗较高的问题展开,介绍了Android中闹钟对齐的作用,并详细分析了基于Android9.0的闹钟对齐的代码和原理。看了ppt后如有问题请发消息给我,很乐意为大家解惑。
android闹钟系统-alarm.doc android闹钟系统-alarm.doc
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包。由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤醒Service发送心跳包。 以下是...
Android闹钟内核驱动——Alarm.pdf Android闹钟内核驱动——Alarm.pdf
Android简单闹钟设置带铃声
android alarmclock 自带闹钟程序源码
android闹钟源码闹钟程序源码15个合集: AlarmClock.tar.gz AlarmTest.zip AndroidRTC-master.zip Android自动开关机实现.pdf Android自动开关机实现.rar ClockPackage.apk-af47e27b7952e0ecccc9b005352d19d20c67eb...
android的闹钟源代码. 简单学习android编程的。请参考它.
android闹钟程序源代码,android系统自带的闹钟程序,下来直接import就可看效果了
闹钟app,android程序
alarm apk 很好用的手机闹钟
闹钟 适用于Android的材质设计闹钟应用程序 谢谢 ( 的 (
一个闹钟的demo 写的很清晰 有需要可以参考一下 适配了魅族手机退出应用后 无法闹铃的bug
android使用闹钟程序来实现定时功能
这是一个(非常简单的)Android 语音闹钟应用程序,它是作为 Android 编程入门课程的家庭作业编写的。 这是java和Android的第一次尝试。 应用使用 有4个按钮可以调整闹钟时间:小时+、小时-、分钟+和分钟-第五个按钮...
Alarm是android提供的用于完成闹钟式定时任务的类,系统通过AlarmManager来管理所有的Alarm,下面这篇文章主要给大家介绍了关于Android中使用Alarm的相关资料,需要的朋友可以参考下。
AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用AlarmManager实现定时提醒...