I'm trying to make an application that changes the UI every minute, but I'm struggling a lot with the postAtTime function because I can't make it trigger. I've tried it with postDelay and it works, but postAtTime should give me more precission.
I get the current time and set a calendar, then add a minute to the date and call the postAtTime, but it does not trigger. Let's see if you could help me.
private Runnable showMinute = new Runnable() {
public void run() {
calendar = Calendar.getInstance();
minute = calendar.get(Calendar.MINUTE);
minute = minute % 10;
switch (minute){
case 0: numero.setImageResource(R.drawable.b0);
break;
case 1: numero.setImageResource(R.drawable.b1);
break;
case 2: numero.setImageResource(R.drawable.b2);
break;
case 3: numero.setImageResource(R.drawable.b3);
break;
case 4: numero.setImageResource(R.drawable.b4);
break;
case 5: numero.setImageResource(R.drawable.b5);
break;
case 6: numero.setImageResource(R.drawable.b6);
break;
case 7: numero.setImageResource(R.drawable.b7);
break;
case 8: numero.setImageResource(R.drawable.b8);
break;
case 9: numero.setImageResource(R.drawable.b9);
break;
default:
break;
}
handler.removeCallbacks(mostrarHora);
calendar.add(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
handler.postAtTime(showMinute, calendar.getTimeInMillis());
}
};
You are using the absolute time (from the Calendar
class). The handler.postAtTime
timeformat is the amount of ms
since startup.
You should use SystemClock.uptimeMillis()
for your calculation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With