学而实习之 不亦乐乎

Android 中定时器 AlarmManager 的使用方法

2022-09-18 15:04:55

一、AlarmManager 简介

AlarmManager 实质是一个全局的定时器,是 Android 中经常使用的一种系统级别的提示服务,在指定时间或周期性启动其他组件(包含Activity,Service,BroadcastReceiver)。

该类提供一种訪问系统闹钟服务的方式,同意你去设置在将来的某个时间点去执行你的应用程序。当闹钟响起(时间到)时,在它上面注册的意图(Intent)将会被系统以广播发出,然后自己主动启动目标程序。

注册的闹钟会被保留,即使设备处于休眠中(假设闹钟在给定时间响起能够选择是否唤醒设备)。闹钟关闭或者重新启动,闹钟将被清除。

当广播的 onReceive() 方法正在运行,闹钟管理者(AlarmManager)会持有一个CPU唤醒锁,保证手机不会休眠直到处理完该广播。一旦 onReceive() 返回,那么闹钟管理者将会释放唤醒锁。这意味着要 OnReceive() 方法执行完毕。手机可能在某些情况下进入休眠。如果你的闹钟广播接收者调用的是 Context.startService(),那么手机有可能在被请求的服务运行之前进入休眠。为了防止这样的情况,你的 BroadcastReceiver 和服务须要实现一个单独的唤醒锁策略以确保手机继续运行,直到服务可用。

注:

  1. 该类适用于应用程序在将来某个指定时间点执行的情况,即使应用程序如今还没有执行。对一般的时间操作,使用 Handler 是更简单有效的。
  2. 由于高版本 Android 对于系统安全性的提高,我们可能无法做到唤起 APP 的目的,不过做一些定时执行的任务还是可以的。

此类公有方法如下:

  • cancel(PendingIntent operation):取消AlarmManager的定时服务。
  • set(int type, long triggerAtTime, PendingIntent operation):设置在triggerAtTime时间启动由operation參数指定的组件。(该方法用于设置一次性闹钟)
  • setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation):设置一个非精确的周期性任务。
  • setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation):设置一个周期性运行的定时服务。
  • setTime(long millis):设置系统“墙”时钟。须要android.permission.SET_TIME.权限。
  • setTimeZone(String timeZone):设置系统的默认时区。须要android.permission.SET_TIME_ZONE.权限。

常用法如下

1、set(int type。long startTime。PendingIntent pi):该方法用于设置一次性闹钟。

    第一个參数int type指定定时服务的类型,该參数接受例如以下值:

  • ELAPSED_REALTIME :在指定的延时过后。发送广播,但不唤醒设备(闹钟在睡眠状态下不可用)。假设在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒。
  •  ELAPSED_REALTIME_WAKEUP :在指定的延时过后。发送广播。并唤醒设备(即使关机也会运行operation所相应的组件)。延时是要把系统启动的时间SystemClock.elapsedRealtime()算进去的。详细使用方法看代码。
  • RTC :指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所相应的设备(在指定的时刻,发送广播,但不唤醒设备)。假设在系统休眠时闹钟触发,它将不会被传递。直到下一次设备唤醒(闹钟在睡眠状态下不可用)。
  • RTC_WAKEUP :指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所相应的设备(在指定的时刻。发送广播,并唤醒设备)。即使系统关机也会运行 operation所相应的组件。

    第二个參数表示闹钟运行时间。
    第三个參数PendingIntent pi表示闹钟响应动作:

  • PendingIntent pi:是闹钟的运行动作,比方发送一个广播、给出提示等等。
  • PendingIntent 是 Intent 的封装类。须要注意的是。假设是通过启动服务来实现闹钟提示的话,PendingIntent 对象的获取就应该採用 Pending.getService(Context c,int i,Intentintent,int j) 方法;假设是通过广播来实现闹钟提示的话。PendingIntent 对象的获取就应该採用 PendingIntent.getBroadcast(Context c,inti,Intent intent,int j) 方法;假设是採用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该採用PendingIntent.getActivity(Context c,inti,Intent intent,int j)方法。假设这三种方法错用了的话。尽管不会报错,可是看不到闹钟提示效果。

2、setRepeating(int type,long startTime,long intervalTime。PendingIntent pi):设置一个周期性运行的定时服务。第一个參数表示闹钟类型,第二个參数表示闹钟首次运行的延迟时间。第三个參数表示闹钟两次运行的间隔时间,第四个參数表示闹钟响应动作(与 Intent 不同的是,PendingIntent 可以脱离应用程序而存在)。

3、setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi):该方法也用于设置反复闹钟。与第二个方法相似,只是其两个闹钟运行的间隔时间不是固定的而已。它相对而言更省电(power-efficient)一些,由于系统可能会将几个差点儿相同的闹钟合并为一个来运行。降低设备的唤醒次数。 第三个參数intervalTime为闹钟间隔,内置的几个变量例如以下:

  • INTERVAL_DAY:设置闹钟,间隔一天
  •  INTERVAL_HALF_DAY:设置闹钟,间隔半天
  • INTERVAL_FIFTEEN_MINUTES:设置闹钟。间隔15分钟
  • INTERVAL_HALF_HOUR:设置闹钟,间隔半个小时
  • INTERVAL_HOUR:设置闹钟。间隔一个小时

二、实例

1、使用 AlarmManager 类定时执行任务

new Intent(getBaseContext(), some_service.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), 0, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// 取消以前同类型的提醒
alarmManager.cancel(pendingIntent);

Calendar calendar = Calendar.getInstance();
Calendar currentDate = Calendar.getInstance();

calendar.add(Calendar.HOUR, 24);
calendar.set(Calendar.HOUR_OF_DAY, 07);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);

// 设定每天在指定的时间运行alert
alarmManager.setRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);

2.使用 AlarmManager 定时发送广播的实例

public class MainActivity extends AppCompatActivity {

    // 声明一个闹钟的广播接收器
    private AlarmReceiver alarmReceiver;
    // 适配Android9.0结束
    
    private Button bt_nz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_nz = findViewById(R.id.bt_nz);
        bt_nz.setOnClickListener((View view) -> {
            // 创建一个广播事件的意图
            Intent intent = new Intent(ALARM_EVENT);
            // 创建一个用于广播的延迟意图
            PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            // 从系统服务中获取闹钟管理器
            AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            // 给当前时间加上若干秒
            calendar.add(Calendar.SECOND, 5);
            // 开始设定闹钟,延迟若干秒后,携带延迟意图发送闹钟广播
            alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
        });
    }

    // 声明一个闹钟广播事件的标识串
    private String ALARM_EVENT = "com.example.senior.AlarmActivity.AlarmReceiver";
    private static String mDesc = ""; // 闹钟时间到达的描述
    private static boolean isArrived = false; // 闹钟时间是否到达

    // 定义一个闹钟广播的接收器
    public static class AlarmReceiver extends BroadcastReceiver {

        // 一旦接收到闹钟时间到达的广播,马上触发接收器的onReceive方法
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (!isArrived) {
                    isArrived = true;
                    //TODO 接收到闹钟到达广播的动作
                    Intent intent1 = new Intent(context, MainActivity.class);
                    context.startActivity(intent1);
                }
            }
        }
    }

    // 适配Android9.0开始
    @Override
    public void onStart() {
        super.onStart();
        // 从Android9.0开始,系统不再支持静态广播,应用广播只能通过动态注册
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 创建一个闹钟的广播接收器
            alarmReceiver = new AlarmReceiver();
            // 创建一个意图过滤器,只处理指定事件来源的广播
            IntentFilter filter = new IntentFilter(ALARM_EVENT);
            // 注册广播接收器,注册之后才能正常接收广播
            registerReceiver(alarmReceiver, filter);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 注销广播接收器,注销之后就不再接收广播
            unregisterReceiver(alarmReceiver);
        }
    }
}