【问题标题】:checking for variable changes within a specified amount of time?在指定的时间内检查变量变化?
【发布时间】:2011-11-14 15:31:34
【问题描述】:

我正在尝试在指定的时间内检查变量更改。例如我有一个变量: int 下载进度 = 0;

我得到了一些下载文件的代码,并且 var downloadProgress 正在更新到 1..2.3....5.6..7。直到 ..100(下载完成)。

现在我想要一个类似下面的线程:

Runnable checkNet = new Runnable(){
    public void run(){
        // check variable changes
    }
};

在指定的时间内检查变量 downloadProgress 的变化,比如 20 秒。如果变量至少在 20 秒内没有改变,那么我想做点什么!

如何在线程中使用计时器来执行此操作!

【问题讨论】:

    标签: java android


    【解决方案1】:

    你可以使用定时器:http://developer.android.com/reference/java/util/Timer.html 在这种情况下你不需要线程,因为 Timer 包含一个线程

    示例如下:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tv = new TextView(this);
    this.setContentView(tv);
    
    MyCount counter = new MyCount(5000,1000);
    counter.start();
    }
    
    public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
    tv.setText(”done!”);
    }
    @Override
    public void onTick(long millisUntilFinished) {
    tv.setText(”Left: ” + millisUntilFinished/1000);
    }
    }
    
    }
    

    来源:http://dewful.com/?p=3

    【讨论】:

    • 所以我会使用 onTick 方法来检查变量变化对吗?
    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2018-01-25
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多