Android的自定义闹钟只有第一次和重复选项,也就是只能响自定义的时间和重复之后的时间(代码会比较容易理解),所以我用了SharePreference记录周几,在自定义的时间响铃。
直接上代码吧。
/**
* 设置闹钟
* @author dongsen
*/
private void setClock(){
//保存时间
edit.putString("repeat_time_goto", gotoTimeTxt);
edit.putString("repeat_time_gooff", goOffTimeTxt);
edit.commit();
//这里都是在处理第一次响铃的时间
long firstTime = SystemClock.elapsedRealtime(); // 开机之后到现在的运行时间(包括睡眠时间)
long systemTime = System.currentTimeMillis();
int mHour = Integer.parseInt(hourString);
int mMinute = Integer.parseInt(minuteString);
//重新将值置空,以便再次使用
hourString = null;
minuteString = null;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 这里时区需要设置一下,不然会有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) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
selectTime = calendar.getTimeInMillis();
}
// 计算现在时间到设定时间的时间差
long time = selectTime - systemTime;
firstTime += time;
// 进行闹铃注册
AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
if (workType == GOTOWORK) {
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 24*60*60*1000l, sender);
}else if (workType == GOOFFWORK) {
PendingIntent senderGoOff = PendingIntent.getBroadcast(this, 1, intent, 0);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 24*60*60*1000l, senderGoOff);
}
}
关键的代码只是这两行
//从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象。
//context,requestCode(可根据这个参数,区分不同闹钟),intent,flag
PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, 0);
//提醒类型,第一次响铃时间,重复时间间隔,intent
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 24*60*60*1000l, sender);
setRepeating第一个参数的解释
AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠;当手机休眠时不发射闹钟。
AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠;
AlarmManager.ELAPSED_REALTIME,真实时间流逝闹钟,不唤醒手机休眠;当手机休眠时不发射闹钟。
AlarmManager.ELAPSED_REALTIME_WAKEUP,真实时间流逝闹钟,当闹钟发躰时唤醒手机休眠;
RTC闹钟和ELAPSED_REALTIME最大的差别就是前者可以通过修改手机时间触发闹钟事件,后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算。
通过Receiver处理闹钟事件
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences spSwitch = context.getSharedPreferences("trackof_setting",Activity.MODE_PRIVATE);
SharedPreferences spWeek = context.getSharedPreferences("trackof_setting", Activity.MODE_PRIVATE);
Calendar calendar = Calendar.getInstance(Locale.CHINA);
//获取当前时间
String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
String minute = String.valueOf(calendar.get(Calendar.MINUTE));
int week = calendar.get(Calendar.DAY_OF_WEEK);
String weekRepeat = spWeek.getString("repeat_repeat", "0,1,2,3,4");
String gotoTxt = spSwitch.getString("repeat_time_goto", null);
String gooffTxt = spSwitch.getString("repeat_time_gooff", null);
boolean goToSwitch = spSwitch.getBoolean("repeat_switch_goto", false);
boolean goOffSwitch = spSwitch.getBoolean("repeat_switch_gooff", false);
String correctWeek = String.valueOf(week -2);
if (hour.length() == 1) {
hour = "0" + hour;
}
if (minute.length() == 1) {
minute = "0" + minute;
}
//是否包含那一天
if (weekRepeat.contains(correctWeek)) {
String nowTxt = hour+":"+minute;
//1.是否通过switch打开提醒 2.时间是否正确
if ( gotoTxt != null && nowTxt.equals(gotoTxt) && goToSwitch ) {
showNotification(context,"上班");
}else if ( gooffTxt != null && nowTxt.equals(gooffTxt) && goOffSwitch ) {
showNotification(context,"下班");
}
}
}
/**
*推送提醒
*/
private void showNotification(Context context,String text){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context,点击推送要打开的界面.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
Notification notifi = new Notification.Builder(context)
.setSmallIcon(R.drawable.logo_small)
.setTicker("您有新的提醒")
.setContentTitle("提醒")
.setContentText(text)
.setContentIntent(pending)
.getNotification();
//设置默认提示音
notifi.defaults = Notification.DEFAULT_SOUND;
notifi.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(1, notifi);
}
}
取消闹钟(也可以通过SharePreference去掉那一天的记录)
Intent intent = new Intent(this, AlarmReceiverWuba.class);
PendingIntent sender = PendingIntent.getBroadcast(this,0, intent, 0);
// 取消闹铃
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
最后别忘了在Manifest中注册
<receiver android:name="路径" >
</receiver>