android AlarmManager 详解
在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接。怎么完成心跳很关键,在说道客户端心跳功能时,如果使用Timer或者专门开起一个线程来做心跳的工作,会浪费CPU工作时间,而且也会更多的消耗电量。相对来说使用AlarmManager 来处理心跳的话,使用的是系统全局的定时服务,会一定成都减少CPU的消耗,耗电量也会少很多。正好这段时间也要做推送,就顺便学习了一下怎么做心跳。
// 取消已经注册的与参数匹配的闹铃
void cancel(PendingIntent operation)
//注册一个新的闹铃
void set(int type, long triggerAtTime, PendingIntent operation)
//注册一个重复类型的闹铃
void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
//设置时区
void setTimeZone(String timeZone)
public static final int ELAPSED_REALTIME
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。
public static final int ELAPSED_REALTIME_WAKEUP
//能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
public static final int RTC
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
public static final int RTC_WAKEUP
//能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
Public static final int POWER_OFF_WAKEUP
//能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为
4(0x00000004)。
AlarmManager处理心跳间隔的相关代码如下:
private AlarmManager mAlermManager;
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final Intent intent = new Intent();
intent.setAction("Const.ACTION_HEARTBEAT
");
intent.putExtra("message", "心跳");
final PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
);
final long time = System.currentTimeMillis();// 设置当前时间
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 8000,pendingIntent);// 设定精确重复提醒
// alarmManager.set(AlarmManager.RTC, time, pendingIntent);// 设置单次闹钟提醒
// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 8000, pendingIntent);// 设置不精确的重复的提醒
// alarmManager.cancel(pendingIntent);// 取消闹钟
定义一个广播接收器,在接收器中处理与服务器的连接等操作:
public class HeartReceiver extends BroadcastReceiver {
private static final String TAG = "HeartReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "action" + action);
if (Const.ACTION_START_HEART.equals(action)) {
Log.d(TAG, "Start heart");
} else if (Const.ACTION_HEARTBEAT.equals(action)) {
Log.d(TAG, "Heartbeat");
//在此完成心跳需要完成的工作,比如请求远程服务器……
} else if (Const.ACTION_STOP_HEART.equals(action)) {
Log.d(TAG, "Stop heart");
}
}
}
android AlarmManager 详解的更多相关文章
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- Android 签名详解
Android 签名详解 AndroidOPhoneAnt设计模式Eclipse 在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...
- Android编译系统详解(一)
++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...
- Android布局详解之一:FrameLayout
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- Android签名详解(debug和release)
Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...
随机推荐
- Css - 文本溢出处理
http://blog.163.com/yinwei_666/blog/static/2036157320101113102733794/ overflow: hidden;-o-text-overf ...
- OpenStack手动从数据库中删除实例 - ugyn109的专栏 - 博客频道 - CSDN.NET
由于某种原因我将OpenStack的一个计算节点移除了,但移除前并没有删除在其上运行的实例,后来想通过dash删除这些实例,于是N天过去了,我的dash还显示如下内容:很碍眼是不是?于是我打算手动从数 ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2build627)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build672 编译日期:2014-12-29 ====================================== 二. ...
- 【转】C#文件操作大全
文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msdn:http://msdn.microsof ...
- 什么JSONP
JSONP 是JSON with padding(填充式JSON 或参数式JSON)的简写,是应用JSON 的一种新方法,在后来的Web 服务中非常流行.JSONP 看起来与JSON 差不多,只不过是 ...
- asp.net webform杂记
context.Response.ContentType = "text/plain"; string encodeFileName = HttpUtilit ...
- PureBasic 打开一个一行有多个数据的文件并读取其中某个数据
如果有一个文件如下: TITLE = "Water Wurface Elevation"VARIABLES = "X", "Y", &quo ...
- git 第一次初始化
Command line instructions Git global setup git config --global user.name "{名字}({工号})" git ...
- 通过驱动向打印机发送一段(ESC)控制指令
这个功能看起来挺奇葩的, 写这个是因为有客户在使用驱动连接票据打印机, 但是又要开钱箱, 驱动里只能每张单据都开钱箱, 而这个打印机又不是只打印结帐单 所以就需要用软件控制打印机开钱箱 票据打印机一般 ...
- docker debug diagnose
$ sudo systemctl stop docker $ sudo docker -d -D DEBU[0282] Error contacting registry https://regist ...