@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } final Window win = getWindow(); win.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // 息屏启动 if (!getIntent().getBooleanExtra("screen_off", false)) { win.addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); } mClockUuid = getIntent().getIntExtra("uuid", -1); Log.d("hu", "传过来的uuid===" + mClockUuid); ClockDBUtil clockDBUtil = new ClockDBUtil(this); clockDBUtil.openDataBase(); clockModel = clockDBUtil.getOneClockByUuid(mClockUuid + ""); clockDBUtil.closeDataBase(); // 启动一分钟响铃计时 new Thread(new MyThread()).start(); // 设置下次闹铃时间 setNextAlarm(); // 给音乐map赋值 setMapData(); // 设置闹铃响 setAlarm(); // 弹出闹铃提示 showDialog(); }
/** 设置下次闹铃响铃时间 */ private void setNextAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); // 建立Intent和PendingIntent来调用闹钟管理器 Intent intent = new Intent(this, AlarmReceiver.class); intent.setAction(AlarmReceiver.ALARM_ACTION); intent.putExtra("uuid", Integer.parseInt(clockModel.getUuid())); Log.d("hu", "设置下次响铃的uuid====" + Integer.parseInt(clockModel.getUuid())); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Integer.parseInt(clockModel.getUuid()), intent, 0); String flag = KEY_OFF; // 看是否有间隔时间 if (!clockModel.getSpace().equals("0")) { // 有间隔时间 // 计算今晚凌晨的时间 long nextDayTime = ClockGetNextAlarm.getTonightLastTime(System.currentTimeMillis()); // 下次响铃时间的毫秒 long nextCurrentTimeMillis = ClockGetNextAlarm.getNextAlarm( System.currentTimeMillis(), clockModel.getTime(), clockModel.getSpace()); if (nextCurrentTimeMillis <= nextDayTime) { // 第二次响铃小于凌晨时间(在当天) // 闹铃响 alarmManager.set(AlarmManager.RTC_WAKEUP, nextCurrentTimeMillis, pendingIntent); flag = KEY_ON; ClockGetNextAlarm.L("showActivity", nextCurrentTimeMillis); } else { // 第二次响铃大于凌晨时间(不在当天,明天) // 每周可以重复响铃的次数 int counts = 7 * 24 / (Integer.parseInt(clockModel.getSpace())); for (int i = 0; i < counts; i++) { // 下次响铃时间的毫秒 if (i == 0) { // 已经是下次响铃时间了 nextCurrentTimeMillis += 0; } else { nextCurrentTimeMillis += Integer.parseInt(clockModel.getSpace()) * AlarmManager.INTERVAL_HOUR; } // 下次响铃时间所在的日期是否包含在重复的日期里 boolean bl = ClockGetNextAlarm.isContainsInRepeat(nextCurrentTimeMillis, clockModel.getRepeat()); if (bl) { // 包含 // 设置响铃时间为此时的时间,并且跳出循环 alarmManager.set(AlarmManager.RTC_WAKEUP, nextCurrentTimeMillis, pendingIntent); ClockGetNextAlarm.L("showActivity", nextCurrentTimeMillis); flag = KEY_ON; break; } else { // 不包含 // 继续循环 flag = KEY_OFF; } } } } else { // 无间隔时间 if (clockModel.getRepeat().equals("7,7,7,7,7,7,7")) { // 代表没有重复日期 // 开关要关掉 flag = KEY_OFF; } else { // 有重复日期 calendar.setTimeInMillis( ClockGetNextAlarm.getNextAlarmTime( ClockGetNextAlarm.DATE_MODE_WEEK, clockModel.getRepeat(), clockModel.getTime())); // 设置闹铃 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); ClockGetNextAlarm.L("showActivity", calendar.getTimeInMillis()); // 开关打开 flag = KEY_ON; } } clockModel.setmSwitch(flag); // 更新开关状态 ClockDBUtil clockDBUtil = new ClockDBUtil(this); clockDBUtil.openDataBase(); clockDBUtil.updateMswitch(Integer.parseInt(clockModel.getmSwitch()), clockModel.getId()); clockDBUtil.closeDataBase(); }