简体   繁体   中英

Alarm Manager setRepeating firing randomly

I've read through most of the posts here about setRepeating alarms, and none seem to fix my issue.

I have two repeating alarms set for a set amount of times as seen here:

    trigger = System.currentTimeMillis()
                + (Integer.parseInt(test) * 60 * 1000);

    workLength = (long) (Integer.parseInt(test) * 60 * 1000);

    breakLeng = (long) (Integer.parseInt(breakLength) * 60 * 1000);

    private void recurringInitialAlarm() {
                // TODO Auto-generated method stub
                // work inital


                    alarm.set(AlarmManager.RTC_WAKEUP, trigger, pintent);
                    SharedPreferences pref = getActivity()
                            .getSharedPreferences("pref", 0);
                    SharedPreferences.Editor edit = pref.edit();

                    System.out.println("initial work alarm set");
                    edit.putString("takeBreak", "true");
                    edit.commit();
                    System.out.println("takeBreak = true");

            }

        private void recurringWorkAlarm() {
                // TODO Auto-generated method stub
                // work recurring

                alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                            System.currentTimeMillis(), (2 * workLength) + breakLeng,
                            pintent3);
                System.out.println("work recurring"
                            + ((2 * workLength) + breakLeng));

            }

        private void recurringBreakAlarm() {
                // TODO Auto-generated method stub
                // break

                alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                            System.currentTimeMillis(), workLength + breakLeng, pintent2);
                System.out.println("break times"
                            + workLength + breakLeng);

            }

where the pending intents are specified by:

Intent intent = new Intent(getActivity(), AlarmReceiver.class);

        final PendingIntent pintent = PendingIntent
                .getBroadcast(getActivity(), 1, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

        final PendingIntent pintent2 = PendingIntent
                .getBroadcast(getActivity(), 2, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

        final PendingIntent pintent3 = PendingIntent
                .getBroadcast(getActivity(), 3, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

The problem is that the two repeating alarms fire about 10 seconds after creation, and times in between randomly. I understand that for KitKat devices, the alarms are not exact and can vary, however, it does properly send a notification around the times afterwards in the long run.

For example when I set both repeating to be exactly one minute after each other:

17:09:20 -- Start alarm manager
17:09:35 -- Both repeating alarm fired
17:10:23 -- One fired
17:11:28 -- One fired
17:12:34 -- One fired, etc...

QUESTION: How can I dismiss these random notifications appearing after ~10 seconds?

The reason the alarms are firing "immediately" (after 15 seconds) is that you schedule the first event for System.currentTimeMillis() - and the AlarmManager does it within 15 seconds which is pretty close.

You need to do something like set the first alarm event for System.currentTimeMillis() + initial_wait_period

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM