我需要显示的持续时间上我的一些活动内的应用。计时开始的,当一项活动开始。

  • 我应该使用服务的计时器?
  • 这是最好的方式吗?
  • 或我应该开始线从一个活动吗?
有帮助吗?

解决方案

我想在使用的情况下你所描述的它是最好的 商店 时间邮票(见 数据储存)和 计算 三角洲GUI使用。如果你需要显示实时钟在一个活动,您可以创建一个独立的线在这项活动只是为了更新。

其他提示

嗯,这取决于有多少接口的工作需要显示您的进展,我将开始一个线内的活动,然后建立一个计时器,检查状态的线的进展并更新接口的需要。服务是良好的背景任务,不需要很多的口通知的/更新。

这里有一个例子是从一个项目目前,我正在工作上(UpdateListRunnable只是呼吁"notifyDataSetChanged()"我的名单上的适配器。我多次在代码所以我封的这一类。此外,updateHandler仅仅是一个经常的处理程序实例):

@Override
public void run() {
    Timer updateProgressTimer = null;
    UpdateItem currentItem = null;

    for(int i = 0; i < items.size(); i++) {
        currentItemIndex = i;
        currentItem = items.get(i);

        if (currentItem.isSelected() == true) {
            updateProgressTimer = new Timer();

            updateProgressTimer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    updateHandler.post(new UpdateListRunnable());
                }
            }, 0, 2000); // check every 2 seconds

            lookupDb.downloadUpdate(currentItem);

            currentItem.setUpToDate(true);
            currentItem.setStatusCode(UpdateItem.UP_TO_DATE);
            currentItem.setProgress(0);
            updateProgressTimer.cancel();

            updateHandler.post(new UpdateListRunnable());
        } // end if its the database we are hosting on our internal server
    } // end for loop through update items

    currentItemIndex = -1;
} // end updateThread run
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top