AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent。而使用Intent的时候,我们还需要它执行一个动作,如startActivity,startService,startBroadcast,才能使Intent有用。通常我们使用PendingIntent,它可以理解为对Intent的封装,包含了指定的动作。

我们可以通过PendingIntent的静态方法得到一个PendingIntent对象,如下:

  1. PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

使用PendingIntent的getBroadcast (Context context, int requestCode, Intent intent, int flags)方法可以得到一个发送广播动作的PendingIntent对象。其中getBroadcast的第4个参数可以为以下4个常量或其他支持使用Intent.fillIn()来控制它的变量:

FLAG_CANCEL_CURRENT:如果描述的PendingIntent对象已经存在时,会先取消当前的PendingIntent对象再生成新的。

FLAG_NO_CREATE:如果描述的PendingIntent对象不存在,它会返回null而不是去创建它。

FLAG_ONE_SHOT:创建的PendingIntent对象只使用一次。

FLAG_UPDATE_CURRENT:如果描述的PendingIntent对象存在,则保留它,并将新的PendingIntent对象的数据替换进去。

接下来看AlarmManager,我们通过以下代码来取得AlarmManager对象。

  1. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

AlarmManager对象中常用的方法有三个:

1、set(int type,long startTime,PendingIntent pi),用于设置一次闹钟。

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

3、setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi),同样是用于设置重复闹钟,但是它是不准确的,相对于第二个方法,也更加节能。因为系统会将差不多的闹钟进行合并,以避免在不必要地唤醒设备。

在上面的三个方法中,type为闹钟的类型,它可以使用以下四个常量:

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

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

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

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

startTime为闹钟开始时间。

intervalTime为闹钟间隔,在第三个方法中,内置的几个变量如下:

INTERVAL_FIFTEEN_MINUTES 
INTERVAL_HALF_HOUR 
INTERVAL_HOUR 
INTERVAL_HALF_DAY 
INTERVAL_DAY

如果我们设定的是发送广播的闹钟,我们还需要写一个广播接收器,并对其进行注册,它才会在闹钟开始的时候接收到广播。

如果要设定启动Activity或Service的闹钟,则在创建PendingIntent的时候,首先Intent对象需设定指定的Activity或Service的class对象,然后对应的调用PendingIntent.getActivity()或PendingIntent.getService()方法。

下面以设置发送广播的闹钟代码实例来看AlarmManager的使用:

首先设定一个闹钟:

  1. Intent intent = new Intent("pw.msdx.ACTION_SEND");
  2. PendingIntent sendIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  3. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  4. am.cancel(sendIntent);
  5. am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 10 * 1000, sendIntent);

在上面的例子中,就会从当前的时间开始,每10分钟启动一次闹钟提醒。需要注意的是,如果设定的开始时间已经过去,它会马上启动闹钟提醒。

接下来需要写一个广播接收器来接收这个广播并进行处理。

代码如下:

  1. public class SendReceiver extends BroadcastReceiver {
  2. public final static String ACTION_SEND = "pw.msdx.ACTION_SEND<span style="font-family: Arial, Helvetica, sans-serif;">";</span>
  3. @Override
  4. public void onReceive(final Context context, Intent intent) {
  5. String action = intent.getAction();
  6. if (ACTION_SEND.equals(action)) {
  7. Log.i("SendReceiver", "send a message");
  8. }
  9. }
  10. }

然后我们还需要注册这个广播接收器,这样它才能接收到广播。这里采用的是静态注册的方法,在AndroidManifest里进行注册:

  1. <receiver
  2. android:name=".SendReceiver"
  3. android:enabled="true"
  4. android:exported="true"
  5. >
  6. <intent-filter>
  7. <action android:name="pw.msdx.ACTION_SEND"/>
  8. </intent-filter>
  9. </receiver>

Android中的AlarmManager的使用的更多相关文章

  1. Android中使用AlarmManager进程被删除的解决办法

    http://blog.csdn.net/zhouzhiwengang/article/details/13022325 在Android中,AlarmManager提供了不受休眠状态的系统定时功能, ...

  2. 转:在Android中使用AlarmManager

    原地址http://blog.csdn.net/maosidiaoxian/article/details/21776697 AlarmManager是Android中的一种系统级别的提醒服务,它会为 ...

  3. 在Android中使用AlarmManager

    AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...

  4. Android中使用AlarmManager设置闹钟

    场景 设置闹钟 闹钟提醒 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建 ...

  5. [Android] 查看Android中的AlarmManager事件

    reference to : https://segmentfault.com/a/1190000000404684 有时候我们需要设置一个alarmmanager事件 但是如果这个事件的时间是凌晨三 ...

  6. Mono For Android中AlarmManager的使用

    最近做了一个应用,要求如下: 程序运行之后的一段时间,分别触发3个不同的事件.当然很快就想到了Android中的AlarmManager和BroadcastReceiver.但是毕竟Mono环境和Ja ...

  7. 一个Demo学完Android中所有的服务(转)

    说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子   1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...

  8. Android开发之AlarmManager详解

    AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver ...

  9. Android 编程下 AlarmManager

    对应 AlarmManager 有一个 AlarmManagerServie 服务程序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册的各类闹铃并适时的设置即将触发的闹铃给闹铃设备 ( 在系 ...

随机推荐

  1. 潭州课堂25班:Ph201805201 django 项目 第四十六课 查错 补缺 (课堂笔记

    从讲项目开始,查找错误,完善笔记,尽可能 翻译没一句代码(以后台为主), 本项目亮点,也是重点 Django ORM中对数据查询的优化(only.defer.select_related) redis ...

  2. WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInvalidArgumentException> -[__NSArrayM objectForKey:]: unrecognized s

    <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...

  3. SourceTree安装教程和破解教程

    SourceTree破解版是一款非常实用的编程工具,这是一款专业的Git和Hg客户端,界面简洁,操作简单易上手,是开发者的必备工具,欢迎大家来绿色资源网下载体验!SourceTree是一款免费的Git ...

  4. HashMap源码分析和应用实例的介绍

    1.HashMap介绍 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.S ...

  5. ES6_入门(5)_解构赋值

    学习参考:http://es6.ruanyifeng.com/#docs/destructuring //2017/7/20 /*对象的解构赋值*/ //对象的解构赋值,当变量名的对象的属性名相同时, ...

  6. js,css文件更新之后,浏览器端还有缓存,久久不能消除

    解决方案,每次更新之后修改下配置信息 /// <summary> /// VersionInfo 版本信息 /// </summary> public static class ...

  7. .Net转Java.05.为啥MySQL没有nolock

    今天忽然想到一个问题,原来为了提高SQL Server性能,公司规定查询语句一般都要加 WITH (NOLOCK)的 现在转Java了,用了MySQL为啥不提这个事情了? 先在MySQL里写了一个查询 ...

  8. SSH error ( Read from socket failed: Connection reset by peer ) and it's solution

    SSH error ( Read from socket failed: Connection reset by peer ) and it's solution ssh cann't connect ...

  9. Java Lambda 表达式 对 Map 对象排序

    Map<String,String> mailParams = new LinkedHashMap<>(); mailParams.put("Action" ...

  10. 为Ubuntu新创建用户创建默认.bashrc并自动加载

    首先,su – 到新创建的用户 拷贝默认的.bashrc过来   1 cp /etc/skel/.bashrc ~/ 然后创建.profile文件   1 vi ~/.profile 粘贴下面的内容 ...