Reputation: 370
I want to checked if set time is less than calender(mobile device)time.
It shows alarms set and if set time is not more that 1.5 hr it shows "ruko ruko" message. If both conditions are false then it will go to else condition.
It does not gave me expected results. Here is my code:
GregorianCalendar mcalender=new GregorianCalendar();
mcalender.set(2013, 7, 17, 13, 0);
if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
}
else if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
}
Upvotes: 0
Views: 149
Reputation: 165
The problem is that mcalender.getTimeInMillis() is checked first. For example, when you alarm is set for 5:30, at 4:30 the first part will be true already, so the else if section is entirely skipped. Just got to reverse the order and that will do :)
if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
//If still have more than 1.5 hours to go (i.e. 2 hours left etc)
Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
}
else if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
//Not more than 1.5 hours, but still not time up yet.
Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
}
else{
//Time is up. Run action.
Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
}
Upvotes: 1