AlarmManager 包含的主要方法:


[java] view plain copy print ?
  1. //取消已经注册的与参数匹配的定时器
  2. voidcancel(PendingIntentoperation)
  3. //注册一个新的延迟定时器
  4. voidset(inttype,longtriggerAtTime,PendingIntentoperation)
  5. //注册一个重复类型的定时器
  6. voidsetRepeating(inttype,longtriggerAtTime,longinterval,PendingIntentoperation)
  7. //注册一个非精密的重复类型定时器
  8. voidsetInexactRepeating(inttype,longtriggerAtTime,longinterval,PendingIntentoperation)
  9. //设置时区
  10. voidsetTimeZone(StringtimeZone)


定时器主要类型:

[java] view plain copy print ?
  1. publicstaticfinalintELAPSED_REALTIME
  2. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3(0x00000003)。
  3. publicstaticfinalintELAPSED_REALTIME_WAKEUP
  4. //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2(0x00000002)。
  5. publicstaticfinalintRTC
  6. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用System.currentTimeMillis()获得。系统值是1(0x00000001)。
  7. publicstaticfinalintRTC_WAKEUP
  8. //能唤醒系统,用法同RTC类型,系统值为0(0x00000000)。
  9. PublicstaticfinalintPOWER_OFF_WAKEUP
  10. //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4(0x00000004)。


下面我们创建一个最简单的alarm程序。 开机启动一个broadcast,然后驱动一个alarm让它每隔1秒钟启动一次。

public class test_chargeReceive extends BroadcastReceiver {

private static AlarmManager mAlarmManager=null;
private static PendingIntent mIdleIntent;
private static final int IDLE_REQUEST = 0;

static long now=0;
static AlertDialog dialog=null ;

@Override
public void onReceive(Context context, Intent intent) {

if(mAlarmManager==null){
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent idleIntent = new Intent("com.keenhi.charge", null);
mIdleIntent = PendingIntent.getBroadcast(context, IDLE_REQUEST, idleIntent, 0);
// mAlarmManager.cancel(mIdleIntent);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 1000, mIdleIntent);

}

String action = intent.getAction();

if (action.equals("com.keenhi.charge")){
mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 1000, mIdleIntent);

now=System.currentTimeMillis();
Log.i("alrm timer now="+now);

}

}

manifest.xml的重要代码:

<receiver android:name="test_chargeReceive">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

<action android:name="com.keenhi.charge"/>
</intent-filter>
</receiver>

上面程序的功能很简单,一是让它开机接收到"android.intent.action.BOOT_COMPLETED"广播就启动,然后建立一个alarm,每隔1秒钟就发一次"com.keenhi.charge"。 为了简单我们自发自收,收到后就显示一个系统的目前的毫秒数。

上面的alarm是有一次,所以我们每次执行时再启动一次alarm。这样做比较通用我们想停就停,间隔时间也可以自由更改。 如果我们只需要实现一个重复的alarm,我们可以这样做。

mAlarmManager.setInexactRepeating

(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000,mIdleIntent);

我们设置一个重复闹钟就可以了,以后都不再需要重复赋值。


更多相关文章

  1. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  2. 显示所有APP的进程详细信息(进程ID、进程所在UID、进程占用内存、
  3. Android之Handler的postDelayed()方法的用法
  4. cron4j 在android实现任务调度
  5. Android调用系统前置相机拍照
  6. Android(安卓)4.4 KitKat NotificationManagerService使用详解与
  7. Android模块化编程——WebView使用之系统返回键设置
  8. 在Android中获取系统正在运行的进程方法
  9. 关于修改Android系统默认语言

随机推荐

  1. 灵魂拷问:为什么做自动化测试的效率总是难
  2. Linux运维入门教程05-01 (正文处理命令及
  3. Hystrix 实战经验分享
  4. 如何使用iMovie对抖动视频进行防抖处理?
  5. centos6.8 update centos7.2
  6. centos6 无法使用yum命令
  7. xxl-job 搭建使用随笔
  8. 浅析liunx负载均衡(apache篇)
  9. flex容器属性的功能,参数,以及作用
  10. mysql数据库CURD中常用操作