Android AlarmManager使用
来源:互联网 发布:完美芦荟胶淘宝官网 编辑:程序博客网 时间:2024/05/01 08:51
AlarmManager:在特定时刻为我们广播发送一个指定intent。
如何使用:
public class AlarmActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm_act); initAlarm(); } private void initAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); int requestCode = 0; /** * 绑定了闹钟的执行动作,比如发送广播、给出提示等; * PendingIntent.getService(Context c, int i, Intent intentm int j) 通过启动服务来实现闹钟提示 * PendingIntent.getBroadcase(...) 通过启动广播来实现 * PendingIntent.getActivity(...) 通过启动Activity来实现 */ PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);// PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),// requestCode, new Intent(this, ViewAnimationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); // 5秒后发送广播 long triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000; /** * set(int type,long startTime,PendingIntent pi); * 设置一次性闹钟 * 第一个参数:闹钟类型 * 第二个参数:闹钟执行的时间 * 第三个参数:闹钟响应动作 * * 5秒后发送广播 */ alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendingIntent); /** * setRepeating(int type,long startTime,long intervalTime,PendingIntent pi); * 设置重复闹钟 * 参数1:闹钟类型 * 参数2:闹钟首次执行时间 * 参数3:闹钟两次执行的时间间隔 * 参数4:闹钟响应动作 * * 5秒后发送广播,然后每隔10秒重复发送 */ //alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, //triggerAtTime, 10 * 1000, pendingIntent); /** * setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi); * 同上:设置重复闹钟 间隔时间不固定,更节能,系统可能将几个差不多的闹钟合并为一个执行,减少唤醒次数 * 参数3:intervalTime * INTERVAL_FIFTEEN_MINUTES * INTERVAL_HALF_HOUR * INTERVAL_HOUR * INTERVAL_HALF_DAY * INTERVAL_DAY */ //alarmManager.setInexactRepeating(); /** * 闹钟取消 */ //alarmManager.cancel(pendingIntent); /** * 闹钟类型: * AlarmManager.ELAPSED_REALTIME: 闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始, SystemClock.elapsedRealtime()),状态值为3; * AlarmManager.ELAPSED_REALTIME_WAKEUP: 闹钟在手机睡眠状态下回唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2; * AlarmManager.RTC: 闹钟在睡眠状态下不可用,改状态下闹钟使用绝对时间,即当前系统时间,状态值为1; * AlarmManager.RTC_WAKEUP: 闹钟在睡眠状态下回唤醒系统并执行提示功能,使用绝对时间,状态值为0; * AlarmManager.POWER_OFF_WAKEUP: 闹钟在手机关机状态下也能正常进行提示功能,使用最多,使用绝对时间(System.currentTimeMillis()),状态值为4,受SDK版本影响。 */ } public static void actionStart(Context context) { Intent intent = new Intent(context, AlarmActivity.class); context.startActivity(intent); }}
0 0
- Android AlarmManager使用
- android AlarmManager的使用
- android AlarmManager的使用
- Android AlarmManager使用
- Android AlarmManager使用
- Android AlarmManager使用
- Android 使用AlarmManager设置闹钟
- Android中的AlarmManager的使用
- android闹钟AlarmManager的使用
- Android中的AlarmManager的使用
- Android中的AlarmManager的使用
- Android中的AlarmManager的使用
- 在Android中使用AlarmManager
- 在Android中使用AlarmManager
- Android中的AlarmManager的使用
- Android中的AlarmManager的使用
- Android—AlarmManager的使用
- Android中的AlarmManager的使用
- Java自带的GUI性能监控工具Jconsole以及JisualVM简介
- Android Fragment 该怎么用?
- 求在一组N个的数中找出第K个最大数
- TensorFlow入门之变量操作
- QML自动循环轮播图
- Android AlarmManager使用
- ASP.Net MVC3安全升级导致程序集从3.0.0.0变为3.0.0.1
- vue.js加载页面出现闪烁问题的解决
- 运维角度浅谈MySQL数据库优化
- Android JNI中C和JAVA代码之间的互相调用
- 一张图看完语法知识
- 线程与线程类
- GPS工作原理
- JAVA相关基础知识