AlarmManager定时闹钟

一、AlarmManager介绍:

AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent。简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent,PendingIntent可以理解为Intent的封装包,简单的说就是在Intent上在加个指定的动作。在使用Intent的时候,我们还需要在执行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的话就是将这个动作包含在内了。

二、AlarmManager的常用方法有三个:

(1)set(int type,long startTime,PendingIntent pi);

该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。

   三个方法各个参数详悉:

(1)int type: 闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;

(2)long startTime: 闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi: 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提 示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

属性或方法名称 说明                                   
 ELAPSED_REALTIME   设置闹钟时间,从系统启动开  始    
ELAPSED_REALTIME_WAKEUP 同上,如果设备休眠则唤醒
INTERVAL_DAY 设置闹钟,间隔一天
INTERVAL_HALF_DAY 设置闹钟,间隔半天
INTERVAL_FIFTEEN_MINUTES 设置闹钟,间隔15分钟
INTERVAL_HALF_HOUR 设置闹钟,间隔半个小时
INTERVAL_HOUR 设置闹钟,间隔一个小时
RTC 设置闹钟时间从系统当前时间开(System.currentTimeMillis())
RTC_WAKEUP 同上,设备休眠则唤醒
set(int type,long triggerAtTime,PendingIntent operation) 设置在某个时间执行闹钟
setRepeating(int type,long triggerAtTime,long interval PendingIntent operation) 设置在某个时间重复执行闹钟
setInexactRepeating(int type,long triggerAtTime,long interval PendingIntent operation)
设置在某个时间重复执行闹钟,但不是间隔固定时间

注意:

  1. 设置闹钟使用AlarmManager.set()函数,它的triggerAtTime参数,如果要用Calendar.getTimesInMillis()获得,就必须先设置Calendar对象,例如要让闹钟在当天的16:30分启动,就要设置HOUR_OF_DAY(16)、MINUTE(30)、MILLISECOND(0),特别是HOUR_OF_DAY,我一开始误用了HOUR,这是12进制计时方法,HOUR_OF_DAY是24进制计时方法。
  2. 针对同一个PendingIntent,AlarmManager.set()函数不能设置多个alarm。调用该函数时,假如已经有old alarm使用相同的PendingIntent,会先取消(cancel)old alarm,然后再设置新的alarm。如何判断是否已经有相同的PendingIntent,请看下条。
  3. 取消alarm使用AlarmManager.cancel()函数,传入参数是个PendingIntent实例。该函数会将所有跟这个PendingIntent相同的Alarm全部取消,怎么判断两者是否相同,android使用的是intent.filterEquals(),具体就是判断两个PendingIntent的action、data、type、class和category是否完全相同。
  4. 在AndroidManifest.xml中静态注册BroadcastReceiver时,一定使用android:process=":xxx"属性,因为SDK已注明:If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the broadcast receiver runs in that process.
  5. 在此讨论一下process属性,它规定了组件(activity, service, receiver等)所在的进程。

      通常情况下,没有指定这个属性,一个应用所有的组件都运行在应用的默认进程中,进程的名字和应用的包名一致。

      比如manifest的package="com.example.helloalarm",则默认进程名就是com.example.helloalarm。

      <application>元素的process属性可以为全部的组件设置一个不同的默认进程。

      组件可以override这个默认的进程设置,这样你的应用就可以是多进程的。                                                                                                                                                      如果你的process属性以一个冒号开头,进程名会在原来的进程名之后附加冒号之后的字符串作为新的进程名。当组件需要时,会自动创建这个进程。这个进程是应用私有的进程。如果process属性以小写字母开头,将会直接以属性中的这个名字作为进程名,这是一个全局进程,这样的进程可以被多个不同应用中的组件共享。 

使用:

三、AlarmManager的使用步骤

1)获得ALarmManager实例 ALarmManager am=(ALarmManager)getSystemService(ALARM_SERVICE);

2)定义一个PendingIntent发出广播

3)调用ALarmManager方法,设置定时或重复提醒

4)取消提醒:

该函数会将所有跟这个PendingIntent相同的Alarm全部取消,怎么判断两者是否相同,android使用的是intent.filterEquals(),具体就是判断两个PendingIntent的action、data、type、class和category是否完全相同。

 四、实例

 通过Service和BroadcastReceiver实现定时任务

(1)写一个Service执行定时发广播操作

public class AlarmService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO: 16/3/28 如果执行费时操作,可以新起线程,但是觉得线程太多不好
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//            }
//        }).start();
        Log.e("test", "这是一条测试信息");
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int minute = 1000;
        long triggerAtTime = SystemClock.elapsedRealtime() + minute;
        Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return super.onStartCommand(intent, flags, startId);
    }
}
(2)写一个BroadcastReceiver接收广播启动Service
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, AlarmService.class);
        context.startService(i);
    }
}

(3)最后在Activity中实现循环
/**
 * 这个Alarm的循环只能给Service用
 * Activity启动Service,Service发送广播,广播接收器接收广播并启动Service,Service再次发送广播,如此重复
 */
public class AlarmActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);
        Intent i = new Intent(this, AlarmService.class);
        startService(i);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent i = new Intent(this, AlarmService.class);
        stopService(i);
    }
}
最后,假如需要对UI进行操作,完全可以将BroadcastReceiver写在Activity中,来实现定时的UI操作。一直启动Service我不觉得是一个好的解决方案,可以定制重复闹钟

参考:https://blog.csdn.net/Jack_EUSong/article/details/51027215

          https://www.cnblogs.com/ProtectedDream/p/6351447.html

         https://blog.csdn.net/violetjack0808/article/details/50982604

        https://blog.csdn.net/wds1181977/article/details/51154026

猜你喜欢

转载自www.cnblogs.com/manmanlu/p/9066442.html