设置闹钟

/**
    * 设置闹钟
    * @param hourOfDay
    * @param minute
    * @return
    */
   public static String setAlam(int hourOfDay, int minute,Context context,int id) {
      // TODO
//    Intent intent = new Intent(context, PenaltyAlarmActivity.class);

      Intent intent = new Intent(action);
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
//    PendingIntent pi = PendingIntent.getActivity(context, id, intent, 0);
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
      Date date = null;
      try {
         date = df.parse(CommonUtil.getYearMouthDayTime()+hourOfDay+":"+minute+":00");
      } catch (ParseException e) {
         e.printStackTrace();
      }
      
      Calendar c = Calendar.getInstance();
      c.setTimeInMillis(System.currentTimeMillis());
      c.set(Calendar.HOUR_OF_DAY, hourOfDay);
      c.set(Calendar.MINUTE, minute);

      String alarm_text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(c
            .getTimeInMillis()));

      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      
      if(hourOfDay < CommonUtil.getHourTime() || (hourOfDay == CommonUtil.getHourTime() && minute < CommonUtil.getMinutesTime())){
         alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis()+24*60*60*1000, pi);
      }else{
         alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
      }
      
      LogUtil.e("设置闹钟=" +df.format(new Date(c.getTimeInMillis())));
      
      return alarm_text;
   }
 
  
 
  
 
  
 
  
 
  
 
  
// 根据设置闹钟的时间,重新设置一下闹钟
alarm_one = ConfigManager.getString(this, Constant.PENALTYONE,
      "0000-00-00 09:00:00");
alarmTwo = ConfigManager.getString(this, Constant.PENALTYTWO,
      "0000-00-00 20:00:00");
alarmThree = ConfigManager.getString(this, Constant.PENALTYTHREE,
      "0000-00-00 10:15:00");

String[] one = (alarm_one.substring(11, 16)).split(":");
String[] two = (alarmTwo.substring(11, 16)).split(":");
String[] three = (alarmThree.substring(11, 16)).split(":");

CommonUtil.setAlam(Integer.parseInt(one[0]), Integer.parseInt(one[1]),
      context, 111);

你可能感兴趣的:(android)