Alarm 是在应用程序生命周期之外设置的,所以它们十分适合于调度定时更新或者数据查询,从而避免了在后台持续运行 Service。但触发 Alarm 时,就会广播指定的 Pending Intent。

Alarm 类型:

1、RTC_WAKEUP:在指定的时间唤醒设备,并激活 Pending Intent。

2、RTC:在指定的时间点激活 Pending Intent,但是不会唤醒设备。

3、ELAPSED_REALTIME:根据设备启动之后经过的时间激活 Pending Intent,但是不会唤醒设备。经过的时间包含设备休眠的所有时间。

4、ELAPSED_REALTIME_WAKEUP:在设备启动并经过指定的时间之后唤醒设备和激活 Pending Intent。

 private void setAlarm() {
/**
* Listing 9-16: Creating a waking Alarm that triggers in 10 seconds
*/
// Get a reference to the Alarm Manager
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE); // Set the alarm to wake the device if sleeping.
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; // Trigger the device in 10 seconds.
long timeOrLengthofWait = 10000; // Create a Pending Intent that will broadcast and action
String ALARM_ACTION = "ALARM_ACTION";
Intent intentToFire = new Intent(ALARM_ACTION);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0,
intentToFire, 0); // Set the alarm
alarmManager.set(alarmType, timeOrLengthofWait, alarmIntent); /**
* Listing 9-17: Canceling an Alarm
*/
alarmManager.cancel(alarmIntent);
} private void setInexactRepeatingAlarm() {
/**
* Listing 9-18: Setting an inexact repeating alarm
*/
//Get a reference to the Alarm Manager
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE); //Set the alarm to wake the device if sleeping.
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; //Schedule the alarm to repeat every half hour.
long timeOrLengthofWait = AlarmManager.INTERVAL_HALF_HOUR; //Create a Pending Intent that will broadcast and action
String ALARM_ACTION = "ALARM_ACTION";
Intent intentToFire = new Intent(ALARM_ACTION);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0,
intentToFire, 0); //Wake up the device to fire an alarm in half an hour, and every
//half-hour after that.
alarmManager.setInexactRepeating(alarmType,
timeOrLengthofWait,
timeOrLengthofWait,
alarmIntent);
}
}

Android 开发工具类 17_setAlarm的更多相关文章

  1. Android开发工具类

    7种无须编程的DIY开发工具 你知道几个? 现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工 具的出现,也帮助 ...

  2. android开发工具类之获得WIFI IP地址或者手机网络IP

    有的时候我们需要获得WIFI的IP地址获得手机网络的IP地址,这是一个工具类,专门解决这个问题,这里需要两个权限: <uses-permission android:name="and ...

  3. android开发工具类总结(一)

    一.日志工具类 Log.java public class L { private L() { /* 不可被实例化 */ throw new UnsupportedOperationException ...

  4. Android 开发工具类 35_PatchUtils

    增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...

  5. Android 开发工具类 13_ SaxService

    网络 xml 解析方式 package com.example.dashu_saxxml; import java.io.IOException; import java.io.InputStream ...

  6. Android 开发工具类 06_NetUtils

    跟网络相关的工具类: 1.判断网络是否连接: 2.判断是否是 wifi 连接: 3.打开网络设置界面: import android.app.Activity; import android.cont ...

  7. Android 开发工具类 03_HttpUtils

    Http 请求的工具类: 1.异步的 Get 请求: 2.异步的 Post 请求: 3.Get 请求,获得返回数据: 4.向指定 URL 发送 POST方法的请求. import java.io.Bu ...

  8. Android 开发工具类 19_NetworkStateReceiver

    检测网络状态改变类: 1.注册网络状态广播: 2.检查网络状态: 3.注销网络状态广播: 4.获取当前网络状态,true为网络连接成功,否则网络连接失败: 5.注册网络连接观察者: 6.注销网络连接观 ...

  9. Android 开发工具类 27_多线程下载大文件

    多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...

随机推荐

  1. 大文件webuploader的基本使用

    webuploader的简单使用 需要的文件   自备  百度很多 webuploader.js  uploader.swf  jQuery <!DOCTYPE html> <htm ...

  2. cxf-rs 和 swagger 的点

    1. 从web.xml 说起 <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class ...

  3. (动态规划 最长有序子序列)Monkey and Banana --HDU --1069

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1069 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. Linq的基本用用法

    Linq 的基本用法: Sort , OrderBy, Skip,Take,Where,Compare,Join,Distinct ,InsertRange 等关键词 Select用法 var sel ...

  5. 在ContextLoaderListener中使用注解注入的类和job中使用注解注入的类

    场景:在ContextLoaderListener子类中加载job,为JobFactory的实现类声明@Component后,在ContextLoaderListener子类中为scheduler设置 ...

  6. web api 多版本控制重要的两个类

    1.版本路径替换 public class ReplaceVersionWithExactValueInPath : IDocumentFilter     {         public void ...

  7. C#数组的定义,不定长的数组?

    首先,在这里我要说明的是,C#中,定义了数组,那么就必须为其指定长度,且他的长度确定,不能够更改.一旦定义一个数组,那么操作系统就在内存中给这个数组指定了一块内存,他是不支持动态分配存储空间的.能够动 ...

  8. HttpWebRequest 模拟浏览器访问网站

    最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...

  9. 详解webpack + vue + node 打造单页面(入门篇)

    1.node下载地址:http://nodejs.cn/download/,安装完成检查node和npm版本 2.淘宝镜像 : npm install cnpm -g --registry=https ...

  10. C# TinyMapper

    https://github.com/TinyMapper/TinyMapper 据说性能蛮高的,我没对比测试过. NuGet安装下. 新增三个实体. public class A { public ...