2011-09-20 42 views
-1

我有一个闹钟设置为每天8点钟用这个代码关闭。AlarmManager启动超过一次报警setRepeating()时间

String alarm = Context.ALARM_SERVICE; 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 8); 
calendar.set(Calendar.MINUTE, 0); 
calendar.set(Calendar.SECOND,0); 
calendar.set(Calendar.MILLISECOND, 0); 

AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm); 
Intent intent = new Intent("NEW_ITEM"); 
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0); 

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender); 
Log.e("RELEASE LIST", "ALARM Set For 1 day from " + calendar.getTimeInMillis()); 

唯一的问题是警报不止一次熄灭。我不想要这个。因为同样的信息会不断重复出现,这会非常烦人。我的代码中是否有东西存在,或者需要做什么来解决这个问题?

编辑:

   if(doc != null){ 

        item = doc.select("tr> td.indexList1, tr > td.indexList2"); 
        if(item != null){ 
         // Iterator over those elements  
        ListIterator<Element> postIt = item.listIterator(); 



        while (postIt.hasNext()) {  


         Element name = postIt.next(); 
         nameOf = name.text(); 


         form = postIt.next().text(); 


         Element url = name.select("a").first(); 
         urlString = url.attr("href"); 


         genre = postIt.next().text(); 


         Date = postIt.next().text(); 

         Log.v("Dates", Date); 



          if(Date.contains(dayOfMonth)){ 

           i++; 


         } 
         } 
         } 
        } 
       return null; 
      } 
      @Override 
      protected void onPostExecute(Void notUsed){ 
       if(i == 0){ 

       } 

       else{ 



    if(i==1){ 
    nm = (NotificationManager) ReleaseService.this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
       CharSequence from = "GameIT"; 
       CharSequence message = "You have "+i +" Today!"; 
       PendingIntent contentIntent = PendingIntent.getActivity(gameReleaseService.this, 0, new Intent(ReleaseService.this, Htmlparser.class), 0); 
       Notification notif = new Notification(R.drawable.icon, 
         "You have "+i +" Released Today!" , System.currentTimeMillis()); 
       notif.defaults |= Notification.DEFAULT_VIBRATE; 
         notif.setLatestEventInfo(ReleaseService.this, from, message, contentIntent); 
         nm.notify(i, notif); 

}else{ 
    nm = (NotificationManager) ReleaseService.this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
       CharSequence from = "GameIT"; 
       CharSequence message = +i+ " released today!"; 
       PendingIntent contentIntent = PendingIntent.getActivity(ReleaseService.this, 0, new Intent(ReleaseService.this, Htmlparser.class), 0); 
       Notification notif = new Notification(R.drawable.icon, 
         "You have "+i+" that released today!" , System.currentTimeMillis()); 
       notif.defaults |= Notification.DEFAULT_VIBRATE; 
         notif.setLatestEventInfo(ReleaseService.this, from, message, contentIntent); 
         nm.notify(i, notif); 




      } 
      } 
      } 
+0

'不止一次'你是什么意思?发生什么事? –

+0

每天8点后正式启动。它像其他几个小时左右一样熄灭。 –

+0

你的代码似乎是正确的..告诉我们关于活动给我们展示一些可能是错误的代码!它有没有重试它正在重试?没有? –

回答

3
public static Calendar getNextDateNotify(Context context) { 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(System.currentTimeMillis()); 
     int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 
     if (dayOfMonth < 15) { 
      cal.set(Calendar.DAY_OF_MONTH, 15); 
     } else { 
      cal.set(Calendar.DAY_OF_MONTH, 1); 
      cal.add(Calendar.MONTH, 1); 
     } 
     //cal.set(Calendar.HOUR_OF_DAY, 12); //? 
     int hour = generateRandomFromTo(9, 20); 
     if (hour < 12) { 
      cal.set(Calendar.AM_PM, Calendar.AM); 
      cal.set(Calendar.HOUR, hour); 
     } else { 
      cal.set(Calendar.AM_PM, Calendar.PM); 
      cal.set(Calendar.HOUR, (hour - 12)); 
     } 
     cal.set(Calendar.MINUTE, 0); 
     cal.set(Calendar.SECOND, 0); 
     // SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy h:mm a"); 
     // Log.d("TAG", df.format(new Date(cal.getTimeInMillis()))); 
     return cal; 
    } 

但是这种方法只适用于我的目的。对于你的你必须修改它。

+0

SO基本上我的是cal.set(Calendar.HOUR_OF_DAY,8);是所有还是我需要更像您的? –

+0

cal.set(Calendar.HOUR_OF_DAY,12); cal.set(Calendar.AM_PM,Calendar.PM);或cal.set(Calendar.AM_PM,Calendar.AM); cal.set(Calendar.HOUR,8); cal.set(Calendar.MINUTE,0); cal.set(Calendar.SECOND,0); cal.add(Calendar.DAY_OF_MONTH,1); //明天早上8:00或晚上8点钟报警 – drifter

+0

我的方法对我来说: 每个月的第一个月和第十五个随机时间上午9点到晚上8点 – drifter

2

我怀疑你有一个以上的报警回事。

您是否检查事件触发时接收到哪个闹钟?

-1

我不知道你的错误是什么。尝试使用一个短的警报,并将其绑定到接收器,您可以重新启动这个短的警报。

public static void scheduleNextAlarm(Context context) { 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 123454322, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Calendar cal = getNextDateNotify(context);//modify it for yourself(add one day for calendar object) 
    if (cal == null) 
     return; 
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    am.set(AlarmManager.RTC, cal.getTimeInMillis(), sender); 
} 

在你的接收机

@Override 
    public void onReceive(Context context, Intent intent) { 
     Utils.scheduleNextAlarm(context); 

我希望这个代码将会对您有用。

+0

我不明白它..谢谢。你能否更详细地解释这与我的问题有关? –

+0

<<在每天8点正式启动后。它像其他几个小时左右一样熄灭。 - >> 用我的上面的代码,我没有任何类似的问题(上面的代码是完全不同的任务,但也许这种方法将对你有用。) – drifter

+0

什么是getNextDateNotifiy()? –