您的位置:首页 > 移动开发 > Android开发

Android闹钟机制实现定时任务功能

2018-01-25 09:02 941 查看

Android的闹钟实现机制, 需要调用AlarmManager.set()将闹铃时间记录到系统中,当闹铃时间到后,系统会给应用程序发送广播,我们只需要去注册广播接收器就可以了。

本文分三部分讲解如何实现闹钟:

目录:
1. 设置闹铃时间;
2. 接收闹铃事件广播;
3. 重开机后重新计算并设置闹铃时间;

1. 设置闹铃时间(毫秒)

private void setAlarmTime(Context context, long triggerAtMillis) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("android.alarm.demo.action");
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//闹铃间隔, 这里设为1分钟闹一次,在第2步我们将每隔1分钟收到一次广播
//int interval = 60 * 1000;
//am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, sender);
}

第二个参数它大致分为两种类型 一种是相对时间 一种是绝对时间。

所以,根据使用的类型不同 triggerAtTime设置也有所不同。

如果使用ELAPSED_REALTIME_WAKEUP类型 应该调用SystemClock.elapsedRealtime()获取相对时间在加上你设定的延迟时间。

2. 接收闹铃事件广播

public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if ("android.alarm.demo.action".equals(intent.getAction())) {
// 第1步中设置的闹铃时间到,这里可以弹出闹铃提示并播放响铃
Toast.makeText(context, "hello alarm", Toast.LENGTH_LONG).show();
System.out.println("hello alarm");
// 可以继续设置下一次闹铃时间;
return;
}
}
}

当然,Receiver是需要在Manifest.xml中注册的:

<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="android.alarm.demo.action" />
</intent-filter>
</receiver>

3. 重开机后重新计算并设置闹铃时间

当然要有一个BootReceiver:

public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
//重新计算闹铃时间,并调第一步的方法设置闹铃时间及闹铃间隔时间
}
}
}

当然,也需要注册:

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

我在设置时钟的时候遇到一点问题
我开始的代码是这样写的

alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender);

我的本意是设定五秒后启动闹钟 但是每次都是我设置完闹钟之后立马就启动了。
后来我发现问题出在第二个参数上 我对他的理解是错误的

我之前以为它是“延迟”时间,而实际它是“启动”时间。
要理解这个参数还要看type这个参数

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) 。

它大致分为两种类型 一种是相对时间 一种是绝对时间。

所以,根据使用的类型不同 triggerAtTime设置也有所不同。

如果使用ELAPSED_REALTIME_WAKEUP类型 应该调用SystemClock.elapsedRealtime()获取相对时间在加上你设定的延迟时间。

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender);

如果使用RTC_WAKEUP类型 应该调用System.currentTimeMillis()获取从1970.1.1号以来的时间在加上你设定的延迟时间

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender);

setRepeating方法有4个参数,这些参数的含义如下:

type:表示警报类型,一般可以取的值是AlarmManager.RTC和AlarmManager.RTC_WAKEUP。如果将type参数值设为AlarmManager.RTC,表示是一个正常的定时器,如果将type参数值设为AlarmManager.RTC_WAKEUP,除了有定时器的功能外,还会发出警报声(例如,响铃、震动)。

triggerAtTime:第1次运行时要等待的时间,也就是执行延迟时间,单位是毫秒。

interval:表示执行的时间间隔,单位是毫秒。

operation:一个PendingIntent对象,表示到时间后要执行的操作。PendingIntent与Intent类似,可以封装Activity、BroadcastReceiver和Service。但与Intent不同的是,PendingIntent可以脱离应用程序而存在。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息