• Android 每天定时提醒功能实现


    android要实现定时的功能那肯定就要用到闹铃相关的技术,

    那么android闹铃实现是基于 AlarmManager 这个类的,首先我们来看一下它的几个主要的方法。

    打开AlarmManager的源码,如下图显示:

    两个核心的方法 :

    private final IAlarmManager mService;
    public void set(int type, long triggerAtMillis, PendingIntent operation) { 
         try {                                                                  
             mService.set(type, triggerAtMillis, operation);                    
         } catch (RemoteException ex) {                                         
         }                                                                      
     }               

     public void setRepeating(int type, long triggerAtMillis,                        
             long intervalMillis, PendingIntent operation) {                         
         try {                                                                       
             mService.setRepeating(type, triggerAtMillis, intervalMillis, operation);
         } catch (RemoteException ex) {                                              
         }                                                                           
     }                                                                               

    第一个方法主要功能是注册一个比较简单的闹铃,第二个方法是注册一个重复的闹铃,这里重复的意思是指:设置5s, 那么每隔5s会执行一次 。

    我们看到这里具体的实现都是基于IAlarmManager的,而IAlarmManager是一个aidl(Android Interface definition language),具体的就不详细介绍了,大家有兴趣的可以自己研究一下。

    下面我来看下set(int type, long triggerAtMillis, PendingIntent operation)方法是如何调用的:

    // 进行闹铃注册
    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
    
    // 过10s 执行这个闹铃
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    
    AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
    manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

    看一下AlarmReceiver 的源码:

    /**
     * 
     * @ClassName: AlarmReceiver  
     * @Description: 闹铃时间到了会进入这个广播,这个时候可以做一些该做的业务。
     * @author HuHood
     * @date 2013-11-25 下午4:44:30  
     *
     */
    public class AlarmReceiver extends BroadcastReceiver {
        
        @Override
        public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "闹铃响了, 可以做点事情了~~", Toast.LENGTH_LONG).show();
        }
    
    }

    还有别忘了,AndroidManifest.xml中需要加入:

    <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" android:process=":remote">

    运行之后,过10s 弹出 "闹铃响了,可以做点事情了~~", 说明成功了。

    ok,这个结果肯定不是我们想要的,我们想要的功能是每天定时提醒的功能,那么需要基于

    setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation)

    这个方法来实现,代码如下:

    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
    
    long firstTime = SystemClock.elapsedRealtime();    // 开机之后到现在的运行时间(包括睡眠时间)
    long systemTime = System.currentTimeMillis();
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    // 这里时区需要设置一下,不然会有8个小时的时间差
    calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    calendar.set(Calendar.MINUTE, mMinute);
    calendar.set(Calendar.HOUR_OF_DAY, mHour);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    // 选择的定时时间
    long selectTime = calendar.getTimeInMillis();
    // 如果当前时间大于设置的时间,那么就从第二天的设定时间开始
    if(systemTime > selectTime) {
    Toast.makeText(MainActivity.this,"设置的时间小于当前时间", Toast.LENGTH_SHORT).show();
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    selectTime = calendar.getTimeInMillis();
    }
    // 计算现在时间到设定时间的时间差
    long time = selectTime - systemTime;
    firstTime += time;
    // 进行闹铃注册
    AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, DAY, sender);
    Log.i(TAG,"time ==== " + time +", selectTime ===== "
    + selectTime + ", systemTime ==== " + systemTime +", firstTime === " + firstTime);
    Toast.makeText(MainActivity.this,"设置重复闹铃成功! ", Toast.LENGTH_LONG).show();

    以上的思路大致是这样的,首先根据设置的时间,算出当前时间离设置时间的时间差,加上这个时间差,我们就知道第一次提醒的时间,然后再设定重复的时间间隔,我们这里设置1天就可以了。

    现在解释一下setRepeating中第一个参数:

    AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠;当手机休眠时不发射闹钟。
    AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠;
    AlarmManager.ELAPSED_REALTIME,真实时间流逝闹钟,不唤醒手机休眠;当手机休眠时不发射闹钟。
    AlarmManager.ELAPSED_REALTIME_WAKEUP,真实时间流逝闹钟,当闹钟发躰时唤醒手机休眠;
    RTC闹钟和ELAPSED_REALTIME最大的差别就是前者可以通过修改手机时间触发闹钟事件,后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算。

    还有,我们这里是调用广播的形式来实现的,也可以用service的方式来进行实现,只要把PendingIntent改动一下即可,相关代码:
    PendingIntent.getService(MainActivity.this, 0,new Intent(MainActivity.this,AlarmService.class), 0);
    其他的均相同。

     点击这里:代码

  • 相关阅读:
    Hibernate4读取Clob数据
    JPA的Embeddable注解
    POJ 2112 Optimal Milking【网络流+二分+最短路】
    HP-UX查看版本
    这里的*号实际表示就是RAC中所有实例都使用
    [置顶] Asp.Net---css样式的使用方式
    使用Understand获取某个函数(方法)的静态度量指标
    机房收费系统的合作版
    C++内存管理学习笔记(5)
    【设计模式】Singleton模式C++实现
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4460075.html
Copyright © 2020-2023  润新知