Android初级教程启动定时器详解
本案例知识是:后台执行定时任务。
Alarm机制:
一、创建LongRunningService类
package com.example.servicebestpractice; import java.util.Date; import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock; public class LongRunningService extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() { @Override
public void run() {
// 打印日志模拟耗时操作。
System.out.println("服务启动时间:" + new Date().toString()); }
}).start(); AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
int times = 1000 * 60;// 设置相隔多久启动一次广播,我设置为1分钟启动一次服务,去执行定时任务(虽然我写的是打印一条日志,看起来很无趣)
long triggerAtime = SystemClock.elapsedRealtime() + times;// 设置触发时间点
Intent i = new Intent(this, AlarmReceiver.class);// 服务启动广播的intent意图
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);// 封装pendingIntent,启动广播接收者意图
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtime,
pendingIntent);// 设置精确定时时间,定时到了触发,广播启动。
return super.onStartCommand(intent, flags, startId);
} }
二、创建要接收上述要启动的广播。
package com.example.servicebestpractice; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
//服务类时间到启动广播这行这个方法
Intent intent2 = new Intent(context, LongRunningService.class);
context.startService(intent2);//启动广播做启动服务操作,服务又一次启动。
//由于服务不再前台,因此不需要设置addFlags();方法。因为服务不再借助任务栈去创建了。
} }
三、我们要有一个启动服务的入口,那就选择在主活动作为入口:
package com.example.servicebestpractice; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//主活动要首先有一次启动服务的操作
Intent intent3 = new Intent(this, LongRunningService.class);
startService(intent3);//启动服务
} }
四、广播、活动、服务三大组件记得去清单文件配置一下:
<activity
android:name="com.example.servicebestpractice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name="com.example.servicebestpractice.LongRunningService" >
</service> <receiver android:name="com.example.servicebestpractice.AlarmReceiver" >
</receiver>
写完这篇博客后,看了一下我的log后台输出如下截图:
每隔一分钟,定时任务完成,启动一次服务。
Android初级教程启动定时器详解的更多相关文章
- Android之Activity启动流程详解(基于api28)
前言 Activity作为Android四大组件之一,他的启动绝对没有那么简单.这里涉及到了系统服务进程,启动过程细节很多,这里我只展示主体流程.activity的启动流程随着版本的更替,代码细节一直 ...
- Android 开机动画启动过程详解
Android 开机会出现3个画面: 1. Linux 系统启动,出现Linux小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片): 2. Android平台启动初始化,出 ...
- Android中Activity启动模式详解
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...
- android学习-Activity启动过程详解
注:只是说明启动activity的过程(ActivityThread如何与ActivityManagerService简称AmS进行进程间通信调用全过程),不解析android从zygote(受精卵) ...
- Android开发——Activity启动模式详解
1. Activity的启动模式 本文原创,转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52054893 1.1 Standard标 ...
- Android 核心分析 之八Android 启动过程详解
Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...
- 【STM32H7教程】第13章 STM32H7启动过程详解
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第13章 STM32H7启动过程详解 本章教 ...
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...
- Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!
Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...
随机推荐
- [APIO2013]
A.机器人 题目大意:给定一个n*m的地图,有一些障碍物和k个机器人,你每次可以选择一个机器人往任意一个方向推,遇到转向器会转向,两个编号相邻的机器人可以合并,求最少推多少次可以全部合并. $n,m\ ...
- mysql 及 posgresql之优劣势大比拼
特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...
- mysql的连接处理过程
在mysqld_main函数中经过一系列的初始化后,mysql开始监听客户端的连接 mysqld_socket_acceptor->connection_event_loop(); 查看my ...
- c语言程序设计第四次作业——顺序结构
(一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...
- Android简单开发的画画板
Android开发画画板要考虑得几个问题如下: 1 屏幕画板.画笔如何绘制问题 2 用户手指触摸屏幕画板监听事件,以及对应的几种状态处理问题 3 保存图片到SD卡,以及在系统相册打开时自动加载刚才的 ...
- TypeScript: Week Reflection
TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...
- MockHttpServletRequestBuilder中content和param的区别
结论: Mock将URL的参数和通过使用param添加的参数添加到request中的parameter中(url参数) 而将content内容.类型并没有进行解析,直接添加到request的conte ...
- java客户端Jedis操作Redis Sentinel 连接池
pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...
- SVN冲突解决
问题一.执行SVN commit时候,会生成除正常文件之外.mine..r3439 ..r3368的三个文件 .mine:是自己要提交的版本 .r3439:在别人之前提交的版本 .r3368:初始版本 ...
- Structured Streaming + Kafka 集成中遇到的问题
官方指导:http://spark.apache.org/docs/2.2.0/structured-streaming-kafka-integration.html 1.版本问题 起初用的kafk ...