Why is my alarm being set recursively?

2019-06-10 07:47发布

I have two values in my Database:

Hour = 10

Minute = 20

EDIT:

By Recursive I dont mean that the alarm is being set for tomorrow and hence forth, the onReceive of my Alarm class is getting called recursively.

I have the following function to set and Alarm using the above values from Database.

public void SetAlarm(Context context){


        retrieve(context); // returns the hour, minute and day value. 
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        Calendar calendarNow = Calendar.getInstance();
        calendarNow.setTimeInMillis(System.currentTimeMillis()); //Now calendar

        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeOneHour));
        calendar.set(Calendar.MINUTE,Integer.valueOf(timeOneMinute) ); 
        calendar.set(Calendar.SECOND, 00);



        if(calendar.after(calendarNow)){
            calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH)));
        }else{
            calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
        }

        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, NotificationsEngine.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000*60*60*24, pi); 




    }

What I simply want is to set an alarm for today if the time (10:20 - this can be changed by the user but right now I am finding problems with this time in particular) has not passed - else set the alarm for tomorrow at the same time.

I first register this alarm in the Mainactivity - which is the start of my application ( I do check if the alarm is already set or not - so that I dont repeat. Then I register the alarm once again after executing my code in service which is invoked by this alarm.

@Override
    public void onReceive(Context context, Intent intent) {

        context.startService(new Intent(context, NotificationService.class));

    }

in the Service(After I execute the code which I intend to be executed by this alarm:

notifEngine.SetAlarm(getApplicationContext());

This issue was not happening till 2014. I presume change in year has triggered something bad!

Edit:

On debugging the following lines seem to be the culprit:

calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
            System.out.println("On Day"+calendar.get(Calendar.DAY_OF_MONTH));

The System.out prints 2 to the console, which ideally should print 3 - I might be wrong here but I think something is wrong here.

So I think I have zeroed down on the issue:

System.out.println("On Day before adding: "+calendar.get(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
            System.out.println("On Day after adding: "+calendar.get(Calendar.DAY_OF_MONTH));

Both the above print commands give an output of 2.

0条回答
登录 后发表回答