Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

alarmmanager - android prevent immediate trigger of alarm service if alarm time has passed for the day

The reference for Alarm Manager says that

If the stated trigger time is in the past, the alarm will be triggered immediately.

I am facing this problem in my application. Here is my alarm manager code :

Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
                pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);

                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);

                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), pendingDinnerIntent);

Is there any workaround to this problem?

-----EDIT------

I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :

Calendar calendar = Calendar.getInstance();
                long currentTime = calendar.getTimeInMillis();
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                long setTime = calendar.getTimeInMillis();
                Timestamp setTimestamp = new Timestamp(setTime);
                Timestamp currentTimestamp = new Timestamp(currentTime);
                if (setTimestamp.after(currentTimestamp))
                {
                    alarmManager.set(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), pendingDinnerIntent);
                }
                else
                {
                }

What should I the alarmManager to in case setTimestamp is before currentTimestamp ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to create Timestamps. You can do it with your Calendar.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);

if(calendar.before(Calendar.getInstance())) {
    calendar.add(Calendar.DATE, 1);
}

alarmManager.set(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), pendingDinnerIntent);

I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...