惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
SecWiki News
SecWiki News
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
P
Palo Alto Networks Blog
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
NISL@THU
NISL@THU
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
小众软件
小众软件
I
Intezer
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
O
OpenAI News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V2EX - 技术
V2EX - 技术
Stack Overflow Blog
Stack Overflow Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 最新话题
博客园 - Franky
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
P
Proofpoint News Feed
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Full Disclosure
Security Latest
Security Latest

博客园 - 风易微凉

AI笔记-快捷键2 AI笔记 快捷键1 AI笔记-钢笔PEN 我的2025 倒影中的你 后座有你 踏入破解行业 一个人、一场梦、一座空城、一生心疼 人间有味是清欢 安卓开发中遇到耗电量高的问题解决方案 no no no no 敢说不 逆向工程-反编译,嵌入 基于项目中遇到的技术问题,谈谈SharedPreferences的使用的注意问题 根据当前进程号,获取进程下线程数目 MongoDB数据库日志备份压缩脚本 mongodb数据库磁盘碎片整理。 mongodb库表信息监控脚本 mongodb mapreduce示例 mongodb表字段处理生成域名字段
AlarmManager的使用
风易微凉 · 2014-10-20 · via 博客园 - 风易微凉

一般我们用timer和AlarmManager进行定时任务。

先简单说下timer的使用:

 1 TimerTask task = new TimerTask(){  
 2       public void run() {  
 3       Message message = new Message();      
 4       message.what = 1;      
 5       handler.sendMessage(message);    
 6    }  
 7 };
 8 timer = new Timer(true);
 9 timer.schedule(task,1000, 1000); //延时1000ms后执行,1000ms执行一次
10 timer.cancel(); //退出计时器

很简单吧。timer不是说明的重点,我们着重说明下AlarmManager如何实现定时任务的。
先说AlarmManager类的关键函数

setRepeating(int type,long startTime,long intervalTime,PendingIntent pi),用于设置重复闹钟。

第一参数:
ELAPSED_REALTIME:闹钟在睡眠状态下不可用,使用的是相对系统启动时间。

ELAPSED_REALTIME_WAKEUP:闹钟在睡眠状态下可用,使用的是相对系统启动时间。

RTC:闹钟在睡眠状态下不可用,使用的是真实时间。

RTC_WAKEUP:闹钟在睡眠状态下可用,使用的是真实时间。*一般我们使用这个*

第二个参数:延迟时间,即延迟多少时间运行第一条定时任务

第三个参数:第一条之后的任务间隔时间;

第四是pendingIntent字面意义:等待的,未决定的Intent。即将要执行的哪个任务,是服务还是activity。

我的使用方法如下:

1 AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
2 Intent intent =new Intent(UplBasicParams.this, AlarmReceiver.class);
3 intent.setAction(GETADTASKACTION);
4 sender=PendingIntent.getBroadcast(UplBasicParams.this, 0, intent, 0);
5 alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),60*1000, sender);

有时我们需要重置定时任务,那么sender要使用同一个才行。
第4行 getBroadcast 因为要调用的是一个receiver,所以要用这个获取pendingIntent,如果要调用的是服务,需要使用getService来获取pi

好了,就说到这里吧。如果有什么技术问题,欢迎大家共同交流 qq群263862916