AlarmManager设置不止一次?

时间:2011-10-07 14:29:31

标签: android

我正在使用此代码在第二天每天8点钟设置闹钟。 我在可以根据用户打开的活动中设置此警报。

//Setting alarm to fire off NEW_GAME intent every 24 hours.
                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);

                 Log.i("Test", "Current time: " + System.currentTimeMillis() );
                    Log.i("Test", "Calendar time: " + calendar.getTimeInMillis() );

                    int currentDate = calendar.get(Calendar.DATE);
                    calendar.set(Calendar.DATE, currentDate+1);

                    Log.i("Test", "Calendar time with a day added: " + calendar.getTimeInMillis() );


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

我唯一的问题是......我们今天上午10点说。我打开明天设置闹钟的活动。让我说我在午夜12点再次打开活动,那天早些时候设置的闹钟会被设置的当前闹钟重写吗?

1 个答案:

答案 0 :(得分:1)

如果在创建PendingIntent对象时使用相同的请求编号(第二个参数)

 PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

然后它将覆盖当前的PendingIntent,因此将替换当前的Alarm。

它还取决于你传递的内容作为它的最后一个参数。常量部分here中给出的可能值。