Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868785
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-10 10:04:53

是在预定的时间触发Intent的,独立于应用程序的提醒用户的方式。当这个触发后,就会广播这个Intent,如果应用程序没有起启,就会启动这个应用程序,而不需要就用程序被打开或者处于活动状态。
通过来管理所有的。
  1. Intent intent = new Intent(this, OneShotAlarm.class);
  2. PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
  3.  
  4. // 设置警报时间
  5. Calendar calendar = Calendar.getInstance();
  6. calendar.setTimeInMillis(System.currentTimeMillis());
  7. calendar.add(Calendar.SECOND, 30);
  8.  
  9. // 设置警报时间,除了用Calendar之外,还可以用
  10. long firstTime = SystemClock.elapsedRealtime();
  11.  
  12. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  13. // 只会警报一次
  14. am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
  15. // 会重复警报多次
  16. am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender);
  17.  
  18. // 要取消这个警报,只要通过PendingIntent就可以做到
  19. am.cancel(sender);

注意:是通过广播intent,所以,Activity,都可以得到其intent,并进行处理

原文作者:沈伟

原文链接:http://www.cnblogs.com/jk1001/archive/2010/08/02/1790167.html

阅读(1021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~