2013-02-18 106 views
0

我正在做一个报警应用程序,根据从日期选择器的日期和时间它必须发挥警报。我曾写过故障在启动报警

 Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.YEAR, year); 
     cal.set(Calendar.MONTH, month); 
     cal.set(Calendar.DAY_OF_MONTH, day); 
     cal.set(Calendar.HOUR, hour); 
     cal.set(Calendar.MINUTE, min); 

     //Create a new PendingIntent and add it to the AlarmManager 
     Intent intent = new Intent(this, AlarmReceiverActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 
      0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager am = 
      (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
       pendingIntent); 

除了在砑光机设置值

代码如果我们硬编码像cal.add值(Calendar.SECOND,5);它发挥警报..如果有人请帮我找到问题。

+0

日历的值是否已正确初始化? – Egor 2013-02-18 11:57:56

+0

是的..如果我们敬酒它显示 – ammukuttylive 2013-02-18 11:58:43

回答

0

你可以尝试这个代码块必须运行,因为我使用这个。

String str_date = "23:59:59"; 
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
      java.util.Date stringToDate = sdf.parse(str_date); 
      long start_time = stringToDate.getTime(); 

      Intent __intent__ = new Intent(this, CleanService.class); 
      PendingIntent __p_intent__ = PendingIntent.getService 
        (getApplicationContext(), 0, __intent__, 0); 
      long interval_time = 86400000; 

      AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
      alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, 
        start_time, interval_time, __p_intent__);