对于setRepeating()时间,AlarmManager不止一次启动Alarm

时间:2011-09-20 14:27:07

标签: android alarmmanager

我有一个警报设置,每天晚上8点用这段代码关闭。

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);




             }
            }
            }

3 个答案:

答案 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;
    }

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

答案 1 :(得分:2)

我怀疑你有不止一个警报。

您是否在触发事件时检查您正在接收哪个警报?

答案 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);

我希望这段代码对你有用。