Android 每天定时提醒功能实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android 每天定时提醒功能实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3913字,纯文字阅读大概需要6分钟。
内容图文
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);
其他的均相同。
点击这里:代码
原文:http://www.cnblogs.com/zhujiabin/p/4460075.html
内容总结
以上是互联网集市为您收集整理的Android 每天定时提醒功能实现全部内容,希望文章能够帮你解决Android 每天定时提醒功能实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。