在服务的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长时间定时任务实现的更多相关文章

  1. Android长时间后台运行Service

         项目需要在后台获取GPS经纬度.当用户对手机有一段时间没有操作后,屏幕(Screen)将从高亮(Bright)变为暗淡(Dim),如果再过段时间没操作, 屏幕(Screen)将又由暗淡(Di ...

  2. Android 长时间运行任务说明

    android 4.0 后,小米手机需要授权 自动启动 (在安全中心权限里设置),不然AlarmManager设置系统闹钟将不起作用

  3. Windows下Android Studio长时间停留在Building "Project Name" Gradle project info画面的解决方法

    问题描述: 创建好一个Android项目后,Android Studio长时间停留在Building [Project Name] Gradle project info画面不动. 原因: 此时And ...

  4. 屏蔽电信流氓广告造成的诡异的问题--Android WebView 长时间不能载入页面

    发如今家里的时候用Android App里的WebView打开站点非常慢,会有十几秒甚至更长时间的卡住. 可是在电脑上打开相同的网页却非常快. 查找这个问题的过程比較曲折,记录下来. 抓取Androi ...

  5. Android下的定时任务

    Android中的定时任务一般有两种实现方式,一种是使用JavaAPI里的Timer类,另一种是使用android的Alarm机制. 这两种方式在多数情况下都能实现类似的效果,但Timer有一个明显的 ...

  6. Handler处理长时间事件

    当我们在处理一些比较长时间的事件时候,比如读取网络或者数据库的数据时候,就要用到Handler,有时候为了不影响用户操作应用的流畅还要开多一个线程来区别UI线程,在新的线程里面处理长时间的操作.开发的 ...

  7. 2018.6.2 AndroidStudio项目中的问题:===== oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间

    oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间 在Android源码中的NotificationManagerService.java这个类中定义了两个静态变量 ...

  8. 【转】开发一个这样的 APP 要多长时间?

    作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 呵呵. 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着 ...

  9. 开发一个这样的 APP 要多长时间?

    作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正 ...

随机推荐

  1. 调度器简介,以及Linux的调度策略

    进程是操作系统虚拟出来的概念,用来组织计算机中的任务.但随着进程被赋予越来越多的任务,进程好像有了真实的生命,它从诞生就随着CPU时间执行,直到最终消失.不过,进程的生命都得到了操作系统内核的关照.就 ...

  2. 使用postman进行并发测试

    1.打开postman软件 左侧栏点击+号键,创建一个并发测试文件夹 2.主面板点击+号键,输入一个测试地址,点击save按钮保存到并发测试文件夹 3.点击三角箭头,再点击Run,弹出Collecti ...

  3. 【mac】ansible安装及基础使用

    安装 环境释放 mac 10.12.5 #more /System/Library/CoreServices/SystemVersion.plist 安装命令 #ruby -e "$(cur ...

  4. 详细分析MySQL的日志(一)

    官方手册:https://dev.mysql.com/doc/refman/5.7/en/server-logs.html 不管是哪个数据库产品,一定会有日志文件.在MariaDB/MySQL中,主要 ...

  5. Perl一行式:处理行号和单词数

    perl一行式程序系列文章:Perl一行式 所有行的行号 $ perl -pe '$_ = "$. $_"' file.log $ perl -ne 'print "$. ...

  6. keras入门(三)搭建CNN模型破解网站验证码

    项目介绍   在文章CNN大战验证码中,我们利用TensorFlow搭建了简单的CNN模型来破解某个网站的验证码.验证码如下: 在本文中,我们将会用Keras来搭建一个稍微复杂的CNN模型来破解以上的 ...

  7. angularjs_百度地图API_根据经纬度定位_示例

    百度API--Demo地址:   http://lbsyun.baidu.com/jsdemo.htm#i8_4 本例是在angular.js使用的百度地图根据经纬度定位的API:(正常的页面写法基本 ...

  8. [转]centos7指定yum安装软件路径

    本文转自:https://www.cnblogs.com/pyyu/p/9814062.html 网上的命令都是垃圾 yum -c /etc/yum.conf --installroot=/opt/a ...

  9. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  10. Integer Partition(hdu4658)2013 Multi-University Training Contest 6 整数拆分二

    Integer Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...