• 【起航计划 018】2015 起航计划 Android APIDemo的魔鬼步伐 17 App->Alarm->Alarm Service


    Alarm Service和Alarm Controller 例子非常类似,只是Alarm Service是用来Schedule一个Service,而前面的例子是来Schedule一个Broadcast。

    前面说过PendingIntent ,可以来描述一个Activity ,Broadcast,或是一个Service。本例是Schedule一个Alarm事件来启动一个Service。这通常用于来执行一个较费时的任务。

    关于如果编写一个Service将在后面的有专门的例子来说明,只里不详述。只要知道AlarmService_Service是一个Service就行了。

    下面的代码用来Schedule一个多次Alarm事件来启动AlarmService_Service:

    private PendingIntent mAlarmSender;
     ...
    // Create an IntentSender that will launch our service, to be scheduled
     // with the alarm manager.
    mAlarmSender = PendingIntent.getService(AlarmService.this,
     0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
    ...
    // We want the alarm to go off 30 seconds from now.
    long firstTime = SystemClock.elapsedRealtime();
     
    // Schedule the alarm!
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
     firstTime, 30*1000, mAlarmSender);

     取消这个Alarm事件:

    // And cancel the alarm.
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.cancel(mAlarmSender);

    代码和Alaram Controller类似,同样的方法也可以Schedule一个Alarm事件来触发一个Activity。

  • 相关阅读:
    IntelliJ IDEA注册码
    linux中patch命令 -p 选项
    设备文件简介
    《算法导论》——矩阵
    《算法导论》——数论
    linux常用查看硬件设备信息命令(转载)
    netstat和telnet命令在Windows7中的用法(转载)
    c++容器使用总结(转载)
    离散数学——数论算法
    c语言中内存对齐问题
  • 原文地址:https://www.cnblogs.com/dongdong230/p/4316495.html
Copyright © 2020-2023  润新知