在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接。怎么完成心跳很关键,在说道客户端心跳功能时,如果使用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. 【转载】jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  2. php数组操作,删除第一个和最后一个元素

    对于一个php数组,该如何删除该数组的第一个元素或者最后一个元素呢?其实这两个过程都可以通过php自带的函数 array_pop 和 array_shift 来完成,下面就具体介绍一下如何来操作. ( ...

  3. DOM下的节点属性和操作小结

    属性: 1 .nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeName,是只读的. 2 .nodeType 值:1,元素节点:2,属性节点:3,文 ...

  4. 验证码识别 edge enhancement - 轮廓增强 region finding - 区域查找

    Computer Science An Overview _J. Glenn Brookshear _11th Edition The task of understanding general im ...

  5. a bitwise operation 广告投放监控

    将随着时间不断增大的数字N个依次编号为1到N的N个球,颜色每次随机为红黑蓝,时间上先后逐个放入篮子中,计算离现在最近的24个球的红.黑.蓝颜色数 广告投放监控 a bitwise operation ...

  6. InnoDB , MyISAM :MySQL 5.7 Supported Storage Engines

    http://dev.mysql.com/doc/refman/5.7/en/storage-engines.html https://en.wikipedia.org/wiki/ACID https ...

  7. 【ECshop错误集锦】解决ECShop发送邮件提示:Error: need RCPT command

    ECShop发送邮件报错Error: need RCPT command,经检测,邮件服务器返回的真实错误是501 mail from address must be same as authoriz ...

  8. toast 防止一直不停弹出,累积显示

    private Toast mToast = null; public void showTextToast(String msg) { if (mToast == null) { mToast = ...

  9. Redis学习二 C#中如何进行这五种数据类型的操作

    我在网上找了好久,就是没有找到Redis和C#结合的书,都是和别的编程语言在一起鬼混. 简单的用C#实现向Redis中插入那我中类型的数据 首先需要到NuGet 里面下载 Redis IDatabas ...

  10. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...