Android长时间定时任务实现
在服务的onStartCommand方法里面使用AlarmManager 定时唤醒发送广播,在广播里面启动服务
每次执行startService方法启动服务都会执行onStartCommand
1、服务定时唤醒 60秒发一次广播
public class MediaService extends Service {
public MediaService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
/*每次调用startService启动该服务都会执行*/
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "启动服务:" + new Date().toString());
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
long triggerTime = SystemClock.elapsedRealtime() + 60000;
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, pi);
return super.onStartCommand(intent, flags, startId);
}
}
采用AlarmManger实现长期精确的定时任务
AlarmManager的常用方法有三个:
- set(int type,long startTime,PendingIntent pi);//一次性
- setExact(int type, long triggerAtMillis, PendingIntent operation)//一次性的精确版
- setRepeating(int type,long startTime,long intervalTime,PendingIntent
pi);//精确重复 - setInexactRepeating(int type,long startTime,long
intervalTime,PendingIntent pi);//非精确,降低功耗
type表示闹钟类型,startTime表示闹钟第一次执行时间,long intervalTime表示间隔时间,PendingIntent表示闹钟响应动作
对以上各个参数的详细解释
闹钟的类型:
- AlarmManager.ELAPSED_REALTIME:休眠后停止,相对开机时间
- AlarmManager.ELAPSED_REALTIME_WAKEUP:休眠状态仍可唤醒cpu继续工作,相对开机时间
- AlarmManager.RTC:同1,但时间相对于绝对时间
- AlarmManager.RTC_WAKEUP:同2,但时间相对于绝对时间
- AlarmManager.POWER_OFF_WAKEUP:关机后依旧可用,相对于绝对时间
绝对时间:1970 年 1月 1 日 0 点
startTime:
闹钟的第一次执行时间,以毫秒为单位,一般使用当前时间。
- SystemClock.elapsedRealtime():系统开机至今所经历时间的毫秒数
- System.currentTimeMillis():1970 年 1 月 1 日 0 点至今所经历时间的毫秒数
intervalTime:执行时间间隔。
PendingIntent :
PendingIntent用于描述Intent及其最终的行为.,这里用于获取定时任务的执行动作。
详细参考译文:PendingIntent
2、接收到广播调用startService启动服务
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MediaService.class);
context.startService(i);
}
}
运行结果:

Android长时间定时任务实现的更多相关文章
- Android长时间后台运行Service
项目需要在后台获取GPS经纬度.当用户对手机有一段时间没有操作后,屏幕(Screen)将从高亮(Bright)变为暗淡(Dim),如果再过段时间没操作, 屏幕(Screen)将又由暗淡(Di ...
- Android 长时间运行任务说明
android 4.0 后,小米手机需要授权 自动启动 (在安全中心权限里设置),不然AlarmManager设置系统闹钟将不起作用
- Windows下Android Studio长时间停留在Building "Project Name" Gradle project info画面的解决方法
问题描述: 创建好一个Android项目后,Android Studio长时间停留在Building [Project Name] Gradle project info画面不动. 原因: 此时And ...
- 屏蔽电信流氓广告造成的诡异的问题--Android WebView 长时间不能载入页面
发如今家里的时候用Android App里的WebView打开站点非常慢,会有十几秒甚至更长时间的卡住. 可是在电脑上打开相同的网页却非常快. 查找这个问题的过程比較曲折,记录下来. 抓取Androi ...
- Android下的定时任务
Android中的定时任务一般有两种实现方式,一种是使用JavaAPI里的Timer类,另一种是使用android的Alarm机制. 这两种方式在多数情况下都能实现类似的效果,但Timer有一个明显的 ...
- Handler处理长时间事件
当我们在处理一些比较长时间的事件时候,比如读取网络或者数据库的数据时候,就要用到Handler,有时候为了不影响用户操作应用的流畅还要开多一个线程来区别UI线程,在新的线程里面处理长时间的操作.开发的 ...
- 2018.6.2 AndroidStudio项目中的问题:===== oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间
oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间 在Android源码中的NotificationManagerService.java这个类中定义了两个静态变量 ...
- 【转】开发一个这样的 APP 要多长时间?
作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 呵呵. 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着 ...
- 开发一个这样的 APP 要多长时间?
作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正 ...
随机推荐
- leetcode — count-and-say
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- 字符串不相同出现相同HashCode(算法)
转自:https://blog.csdn.net/fly_grass_fish/article/details/81742794 在Java中有HashCode的说法,以前以为HashCode是唯一的 ...
- NLP入门(二)探究TF-IDF的原理
TF-IDF介绍 TF-IDF是NLP中一种常用的统计方法,用以评估一个字词对于一个文件集或一个语料库中的其中一份文件的重要程度,通常用于提取文本的特征,即关键词.字词的重要性随着它在文件中出现的 ...
- 【转】没那么难,谈CSS的设计模式
什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的解释:“设计模式(Design p ...
- [转]PHP开发者必须了解的工具—Composer
本文转自:https://blog.csdn.net/Zhihua_W/article/details/80345973 Composer是PHP 用来管理依赖(dependency)关系的工具.你可 ...
- Base64字符保存图片,图片转换成Base64字符编码
//文件转换成Base64编码 public static String getFileBase64Str(String filePath) throws IOException { String f ...
- Mac下写博客工具MarsEdit相关资料
参考资料: https://www.maoshu.cc/967.html 下载地址: https://www.red-sweater.com/marsedit/ 博客园的配置: http://www. ...
- awesome python 中文版 相见恨晚!
awesome python 中文版 相见恨晚! https://www.zhihu.com/question/24590883 这篇知乎厉害了!一定要学习! 作者:知乎用户链接:https:// ...
- PHP常用函数归类【持续整理中......】
一.PHP基础语法 变量,常量 严格区分大小写,但内置结构或关键字无所谓(echo) 命名:不能以数字,空格,.来开头,但是可以有汉字,eg:$变量="aa"; ...
- IO学习一(File类)
File类 1.凡是与输入.输出相关的类.接口都定义在java.io包下 2.File有构造器来创建对象,此对象对应着一个文件或文件目录 支持文件类型:.txt .avi .doc .jpg .ppt ...