Android用AlarmManager实现后台任务-android学习之旅(63)
因为Timer不能唤醒cpu,所以会在省电的原因下失效,所以需要唤醒cpu在后台稳定化的执行任务,AlarmManager能够唤醒cpu
这个例子讲解了如何通过Service来在后他每一个小时执行.特定的任务,原理是在Service里面设置一个一小时定时,然后到了一个小时以后启动BroadCast,然后在onReceive方法中再次启动Service,这样每一个小时就启动一次Service,行程循环
代码如下
activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,LongRunningService.class);
startService(intent);
}
service
public class LongRunningService extends Service {
public LongRunningService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
Log.d("LongRunningService", "excuted" + new Date().toString());
}
}).start();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int anHour = 60*60*1000;
long trigerAtTime = SystemClock.elapsedRealtime()+anHour;
Intent alarmIntent = new Intent(this,AlarmReceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,alarmIntent,0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,trigerAtTime,pendingIntent);
return super.onStartCommand(intent, flags, startId);
}
}
receiver
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Intent i = new Intent(context,LongRunningService.class);
context.startService(i);
}
}
Android用AlarmManager实现后台任务-android学习之旅(63)的更多相关文章
- 开发了5年android,我开始了go学习之旅
前言 做了近5年的android开发,最近项目也是不怎么忙,空闲的时候总会思考一些事情,不过作为移动开发,我个人觉得很有必要学习后台开发,由于公司是Go语言开发的,了解go语言一段时间后,我发现go语 ...
- Android列表视图ListView和ListActivity-android学习之旅(二十四)
ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...
- Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)
ViewAnimator继承了FrameLayout,多个组件重合在一起,可以加入多个组件,然后切换的时候会有动画. ViewAnimator及其子类的继承关系 ViewAnimator常用属性 Vi ...
- Android中AlarmManager使用示例(持续更新,已经更改)
现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...
- Android M Permission 运行时权限 学习笔记
Android M Permission 运行时权限 学习笔记 从Android 6.0开始, 用户需要在运行时请求权限, 本文对运行时权限的申请和处理进行介绍, 并讨论了使用运行时权限时新老版本的一 ...
- Android课程---关于数据存储的学习(2)
手机外部存储的学习 activity_data2.xml <?xml version="1.0" encoding="utf-8"?> <Li ...
- Android闹钟 AlarmManager的使用
Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...
- Android 的 AlarmManager 和 wakeLock联合使用
http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up 主要说的是,对于android ...
- 42.Android之ListView中ArrayAdapter简单学习
今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...
随机推荐
- SpringMVC mock测试详解
@RunWith(SpringRunner.class) @SpringBootTest(classes = WebmanagerApplication.class) //配置事务的回滚,对数据库的增 ...
- Oracle中备份用户对象的两种方法
方法1: 执行步骤: exp userid=用户名/密码@数据库名 file=c:\emp.dmp 使用当前用户导出 exp userid=sys/sys@数据库名 file=c:\emp.dmp o ...
- 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)
总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...
- JS实现2048代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- jQuery 遍历 – 过滤
缩小搜索元素的范围 三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素. 其他过滤方法,比如 filter() 和 not() ...
- MySQL LIKE 子句
MySQL LIKE 子句 我们知道在MySQL中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录. WHERE 子句中可以使用 ...
- Docker容器如何互联
容器的连接(linking)系统是除了端口映射外,另一种跟容器中应用交互的方式. 该系统会在源和接收容器之间创建一个隧道,接收容器可以看到源容器指定的信息. 自定义容器命名 连接系统依据容器的名称来执 ...
- Win7 环境下虚拟机内 Samba 服务器的安装、配置以及与主机的通信实现
考虑到window和linux虚拟机之间互传文件较为麻烦,遂打算在虚拟机中安装Samba服务器,以此实现共享文件给window使用.然而安装配置过程曲折,遂作记录如下: 一.samba服务器的安装 正 ...
- mongo 写分析
写操作 复制集 mongo所有的节点都是写入到primary节点,同时写入oplog,secondary 节点会持续的从primary节点上复制oplog的信息,然后根据oplog写数据.second ...
- 如何将一个二进制的xxx.bin文件轻松转为C语言数组
今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体.通过SD卡写到SPI- ...