在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接。怎么完成心跳很关键,在说道客户端心跳功能时,如果使用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 详解的更多相关文章

  1. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  2. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  3. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  4. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  5. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

  6. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  7. 【整理修订】Android.mk详解

    Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...

  8. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

  9. Android签名详解(debug和release)

    Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...

随机推荐

  1. ElasticSearch实战-日志监控平台

    1.概述 在项目业务倍增的情况下,查询效率受到影响,这里我们经过讨论,引进了分布式搜索套件——ElasticSearch,通过分布式搜索来解决当下业务上存在的问题.下面给大家列出今天分析的目录: El ...

  2. POJ 1703 Find them, Catch them(种类并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: ...

  3. C3P0连接池问题,APPARENT DEADLOCK!!! Creating emergency..... [问题点数:20分,结帖人lovekong]

    采用c3p0连接池,每次调试程序,第一次访问时(Tomcat服务器重启后再访问)都会出现以下错误,然后连接库需要很长时间,最终是可以连上的,之后再访问就没问题了,请高手们会诊一下,希望能帮小弟解决此问 ...

  4. SSH+Oracle10G抛Disabling contextual LOB creation as createClob() m

    在使用Oracle10G时候,实体类使用了CLOB字段,结果抛了Disabling contextual LOB creation as createClob() method threw error ...

  5. 浏览器 user-agent 字符串的故事

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? 故事还得从头说起,最初的主角叫NCSA Mosaic,简称Mosaic(马赛克),是1992年末位于伊利诺伊大 ...

  6. PHPStorm下XDebug配置

    PHPStorm下XDebug配置 分类: PHP2013-08-11 22:15 19697人阅读 评论(0) 收藏 举报   目录(?)[+]   1安装Xdebug 用yum安装可能会失败,用p ...

  7. varnish中忽略cookie进行缓存

    varnish不缓存cookie的页面,如果html页面中带有cookie以下代码为接收到结尾的文件,自动去除掉cookiesub vcl_recv {    if (req.request == ” ...

  8. 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的

    while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...

  9. 【转】说说如何使用unity Vs来进行断点调试

    大家可以从这下载最新版的unity vs. UnityVs1.81下载  1.   安装unity vs.首先我们打开我们下载的unity vs.然后就会看见里面有3个文件,我们双击UnityVS 2 ...

  10. MVC中JQuery文件引入的路径问题,@Url.Content函数

    今天写了个MVC的Demo,文件夹结构很简单,如下: 利用EF生成Model框架并手工加表字段注解,但在页面上JS验证始终没显示.实在无语. 无意中在浏览器里按F12,看见提示:   Failed t ...