4

我正在尝试制作一个每分钟更改 UI 的应用程序,但我在 postAtTime 函数上遇到了很多困难,因为我无法触发它。我已经用 postDelay 尝试过它并且它有效,但 postAtTime 应该给我更多的精确度。

我获取当前时间并设置日历,然后在日期上添加一分钟并调用 postAtTime,但它不会触发。让我们看看你能不能帮助我。

   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());
    }
  };
4

2 回答 2

10

您正在使用绝对时间(来自Calendar班级)。handler.postAtTime时间格式是ms自启动以来的数量。

你应该使用SystemClock.uptimeMillis()你的计算。

于 2012-06-15T15:14:27.737 回答
4

好吧,最后我解决了。如果有人遇到同样的问题,这就是解决方案。

 private Runnable mostrarHora = new Runnable() {
    public void run() {
        int segundo;
        long start, diferencia;
        calendar = Calendar.getInstance();
        diferencia = calendar.getTimeInMillis()-SystemClock.uptimeMillis();
        segundo = calendar.get(Calendar.MINUTE);
        segundo = segundo % 10;

        switch (segundo){
        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.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+1);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        start = calendar.getTimeInMillis()-diferencia;
        handler.postAtTime(mostrarHora, start);
    }
  };
于 2012-06-15T16:09:32.797 回答