Android基于AlarmManager实现用户在线心跳功能示例
作者:jia635 时间:2021-12-02 20:26:46
本文实例讲述了Android基于AlarmManager实现用户在线心跳功能。分享给大家供大家参考,具体如下:
在做即时通信或者其他检测是否在线等操作时要用到心跳。比较常用的是AlarmManager全局定时器 去实现。
AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作
AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.
下面的代码详细的介绍了两种定时方式的使用:
(1)在指定时长后执行某项操作
//操作:发送一个广播,广播接收后Toast提示定时操作完成 Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("short");
PendingIntent sender= PendingIntent.getBroadcast(Main.this, 0, intent, 0);
//设定一个五秒后的时间
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//或者以下面方式简化
//alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
Toast.makeText(Main.this, "五秒后alarm开启", Toast.LENGTH_LONG).show();
注意:receiver记得在manifest.xml注册
public static class alarmreceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("short")){
Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "repeating alarm", Toast.LENGTH_LONG).show();
}
}
}
(2)周期性的执行某项操作
Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent
.getBroadcast(Main.this, 0, intent, 0);
//开始时间
long firstime=SystemClock.elapsedRealtime();
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);//5秒一个周期,不停的发送广播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 5*1000, sender);
AlarmManager的setRepeating()
相当于Timer的Schedule(task,delay,peroid);
有点差异的地方是Timer这个方法是指定延迟多长时间以后开始周期性的执行task;
AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)
Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent.getBroadcast(Main.this, 0, intent, 0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/jia635/article/details/45577563
标签:Android,AlarmManager
0
投稿
猜你喜欢
关于idea更新到2020.2.3无法创建web项目原因 library is not specified
2022-11-24 10:13:28
C#调用动态库
2021-07-30 03:46:25
C#下实现创建和删除目录的实例代码
2021-10-21 16:04:31
Android三方依赖冲突Gradle中exclude的使用
2023-05-05 05:09:56
Java中logback 自动刷新不生效的问题解决
2022-09-07 21:26:04
MyBatis动态Sql之if标签的用法详解
2023-04-20 14:02:26
C#实现简单的天气预报示例代码
2022-03-22 22:52:59
Android开机画面的具体修改方法
2023-05-13 20:52:22
C#中执行批处理文件(*.bat)的方法代码
2022-12-01 10:25:01
Android使用fragment实现左侧导航
2023-10-30 10:02:09
Android中ListView的几种常见的优化方法总结
2021-06-09 08:36:21
详解Java程序启动时-D指定参数是什么
2021-10-22 07:35:34
Android选择图片或拍照图片上传到服务器
2022-12-15 01:48:41
Java中的Object类用法总结
2023-06-06 11:48:36
Java构造器与传值学习总结
2023-03-08 23:18:06
C# JWT权限验证的实现
2022-11-24 00:57:13
SpringBoot整合mybatis/mybatis-plus实现数据持久化的操作
2023-10-27 19:19:45
C#把UNICODE编码转换为GB编码的实例
2023-03-11 09:28:25
详解Spring-bean的循环依赖以及解决方式
2023-08-18 18:30:38
C#多线程基础知识汇总
2023-03-06 08:35:47