免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1005 | 回复: 0
打印 上一主题 下一主题

Android---AlarmManager(全局定时器/闹钟) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览

转自:http://www.cnblogs.com/jico/archive/2010/11/03/1868361.html

AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作

AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.

下面的代码详细的介绍了两种定时方式的使用:

 (1)在指定时长后执行某项操作

代码 

 

//操作:发送一个广播,广播接收后Toast提示定时操作完成
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction(
"short");
PendingIntent sender
=
PendingIntent.getBroadcast(Main.
this, 0, intent, 0);

//设定一个五秒后的时间
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND,
5);

AlarmManager alarm
=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//或者以下面方式简化
//alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);

Toast.makeText(Main.
this, "五秒后alarm开启", Toast.LENGTH_LONG).show();

//注意:receiver记得在manifest.xml注册

 public static class alarmreceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("short")){
Toast.makeText(context,
"short alarm", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(context,
"repeating alarm",
Toast.LENGTH_LONG).show();
}
}
}

(2)周期性的执行某项操作

代码
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction(
"repeating");
PendingIntent sender
=PendingIntent
.getBroadcast(Main.
this, 0, intent, 0);
//开始时间
long firstime=SystemClock.elapsedRealtime();

AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
  //5秒一个周期,不停的发送广播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
, firstime,
5*1000, sender);

 AlarmManager的setRepeating()相当于Timer的Schedule(task,delay,peroid);有点差异的地方时Timer这个方法是指定延迟多长时间

以后开始周期性的执行task;

AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)

代码
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction(
"repeating");
PendingIntent sender
=PendingIntent
.getBroadcast(Main.
this, 0, intent, 0);
AlarmManager alarm
=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP