zoukankan      html  css  js  c++  java
  • android学习笔记--AlarmManager

    AlarmManager称呼为全局定时器,有的称呼为闹钟。其实它的作用和Timer有点相似。

    都有两种相似的用法:

    (1)在指定时长后执行某项操作(2)周期性的执行某项操作

    AlarmManager 包含的主要方法:

    // 取消已经注册的与参数匹配的定时器
    void cancel(PendingIntent operation)
    //注册一个新的延迟定时器
    void set(int type, long triggerAtTime, PendingIntent operation)
    //注册一个重复类型的定时器
    void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
    //注册一个非精密的重复类型定时器
    void setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
    //设置时区
    void setTimeZone(String timeZone)

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

    android提供了的几种类型的闹钟: 

    public static final int ELAPSED_REALTIME
    在指定的延时过后,发送广播,但不唤醒设备。// 当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时 间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。

    public static final int ELAPSED_REALTIME_WAKEUP
    在指定的延时后,发送广播,并唤醒设备 //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。

    public static final int RTC
    在指定的时刻,发送广播,但不唤醒设备 //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()获得。系统值是1 (0x00000001) 。

    public static final int RTC_WAKEUP
    在指定的时刻,发送广播,并唤醒设备//能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。

    Public static final int POWER_OFF_WAKEUP
    //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4(0x00000004)。

     见api/app/alarm/AlarmController实例

    demo:

    AlarmManager mgr = (AlarmManager) context
    .getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(context, MyAlarmService.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);

    //设置触发时间

    GregorianCalendar now = new GregorianCalendar();
    GregorianCalendar targetTime = new GregorianCalendar();

    targetTime.set(GregorianCalendar.HOUR_OF_DAY, 2);
    targetTime.set(GregorianCalendar.MINUTE, 0);

    long diff = targetTime.getTimeInMillis() - now.getTimeInMillis();

    if (diff < 0) // passerat 2am
    diff += AlarmManager.INTERVAL_DAY;

    //注册一个重复类型的定时器
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + diff,
    AlarmManager.INTERVAL_DAY, pi);

  • 相关阅读:
    Building a flexiable renderer
    Indirect Illumination in mental ray
    我的心情
    Cellular Automata
    Subsurface Scattering in mental ray
    Shader Types in mental ray
    BSP Traversal
    我的渲染器终于达到了MR的速度
    How to handle displacement and motion blur
    说明
  • 原文地址:https://www.cnblogs.com/yuan1225/p/3806031.html
Copyright © 2011-2022 走看看