Android 闹钟设置详解
2016-06-11 22:50
435 查看
一、闹钟的分类
1)从闹钟的设置时间方式分为:以开启启动后的间隔时间和日历时间2)从硬件上来说分为:1、当cpu休眠时不启动 2、即时cpu休眠时仍然启动
所以有如下四种:
ELAPSED_REALTIME—Fires the pending intent based on the amount of time since the device was booted, but doesn’t wake up the device. The elapsed time includes any time during which the device was asleep.即按照启动后的时间计算并且当手机休眠时不被唤醒
ELAPSED_REALTIME_WAKEUP—Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.即按照启动后的时间计算并且当手机休眠时依然能被唤醒
RTC—Fires the pending intent at the specified time but does not wake up the device. 即按照真正的日历时间计算,休眠时不能被唤醒
RTC_WAKEUP—Wakes up the device to fire the pending intent at the specified time. 即按照真正的日历时间计算,休眠时可以被唤醒
二、如何设置闹钟
1)设置ELAPSED闹钟privateAlarmManager alarmMgr; privatePendingIntent alarmIntent; //首先获取闹钟的service alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent =new Intent(context,AlarmReceiver.class); //设置PendingIntent 即闹铃启动后要做的事情 alarmIntent =PendingIntent.getBroadcast(context,0, intent,0); //设置闹铃 alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+ 60*1000, alarmIntent);
2)设置RTC方式启动
Calendar calendar =Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY,14); alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); 若需要精确的时间则可以设置:setRepeating()
三、如何使闹钟开机启动
1)设置开机启动<receiver android:name=".SampleBootReceiver" android:enabled="false"> <intent-filter> <actionandroid:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver>
3)通过设置packmange的setComponentEnabledSetting属性开启
ComponentName receiver =new ComponentName(context,SampleBootReceiver.class); PackageManager pm = context.getPackageManager(); //手动的设置ComponentName的属性为不被杀死 pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 这样即使重启闹钟依然有效
相关文章推荐
- Android四大组件--Broadcast Receiver
- Retrofit初体验
- ViewPager 详解(一)---基本入门
- Android初级教程三个Dialog对话框小案例
- Android初级教程三个Dialog对话框小案例
- Android Photo&Vedio 视频拍摄和照片拍摄
- Android蓝牙BLE(一)--使用介绍
- Android 获取系统图库的图片
- Android LayoutInflater详解
- Android Studio 开发使用总结
- Android问题集锦之四十九:Can't add XStream file dependency to Android Studio project
- Android Studio的下载和安装教程(从ADT到AS)
- Android之GLES2.0控制立方体旋转测试代码
- Android 有用但是不常用到的类
- Android开源框架真的是捷径……开发吗
- 【Android】数据存储之文件存储
- Android 读取后台数据并显示。模拟小区车辆管理系统
- Android单元测试
- Android中获取手机中的联系人信息
- java/android 设计模式学习笔记(9)---代理模式