Reputation: 2531
i am trying to implement scheduling in my app which runs daily on set time.scheduler runs fine for the next coming hours but if i am setting it for time elapsed and set it to start scheduler runs instantly.Here is the code I have even added the code to increase the number of milli sec in case the time has already passed but still not working.
OnTimeSetListener onTimeSetListener = new OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
int timeDifference = 0;
int splithour = 0;
int splitmin = 0;
String[] splitsecond = null;
Calendar calNow = Calendar.getInstance();
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
calNow.set(Calendar.MINUTE, minute);
calNow.set(Calendar.SECOND, 00);
calNow.set(Calendar.MILLISECOND, 00);
etTime.setText(simpDate.format(calNow.getTime()));
String[] splitstrtime = simpDate.format(calNow.getTime()).split(":");
splithour = Integer.parseInt(splitstrtime[0]);
splitmin = Integer.parseInt(splitstrtime[1]);
splitsecond = splitstrtime[2].split(" ");
if (splitsecond[1].equalsIgnoreCase("pm")) {
timeDifference = 12;
if (splitsecond[1].equalsIgnoreCase("pm")
&& splithour == 12) {
timeDifference = 0;
splithour = 12;
}
} else if (splitsecond[1].equalsIgnoreCase("am")) {
timeDifference = 0;
if (splitsecond[1].equalsIgnoreCase("am")
&& splithour == 12) {
timeDifference = 0;
splithour = 0;
}
}
splithour = timeDifference + Integer.parseInt(splitstrtime[0]);
calNow.set(Calendar.HOUR_OF_DAY, splithour);
System.out.println(splithour);
calNow.set(Calendar.MINUTE, splitmin);
System.out.println(splitmin);
calNow.set(Calendar.SECOND,Integer.valueOf(splitsecond[0]));
etTime.setText(simpDate.format(calNow.getTime()));
Seconds=calNow.getTimeInMillis();
System.out.println("Current"+Seconds);
if(Seconds< System.currentTimeMillis())
{
Seconds=Seconds+1*24*60*60;
System.out.println("Alter"+Seconds);
}
}};
Upvotes: 0
Views: 678
Reputation: 8145
Your approach is right, you need to add a day to your time if the time has already passed since you want the alarm to trigger tomorrow. You did one mistake though, and it's that you are adding seconds when it should be milliseconds. Notice that your variable Seconds contains milliseconds:
Seconds=calNow.getTimeInMillis();
But you are adding seconds instead of milliseconds:
Seconds=Seconds+1*24*60*60;
Try it like this:
Seconds=Seconds+1*24*60*60*1000;
Upvotes: 1