1

我正在使用 System.NanoTime 来跟踪经过的时间,但它不会在 UI 上更新。以下是我的代码:

然而,我在方法中做所有事情,onCreate()我采用的方法可能并不可靠,但这就是我想要更多想法的地方。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    long startTime = System.nanoTime();
    // ... the code being measured ...
    long estimatedTime = (System.nanoTime() - startTime) / 1000000000;
    System.out.println(""+estimatedTime);
    while (estimatedTime <= 100){
        System.out.println(""+estimatedTime);

        if(estimatedTime == 1){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing"); 

        }
        if(estimatedTime == 2){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Inatializing"); 

        }
        if(estimatedTime == 3){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing to install"); 

        }
        if(estimatedTime == 4){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installing"); 

        }
        if(estimatedTime == 5){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installed"); 

        }
        if(estimatedTime == 6){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Unzipping packages..."); 

        }


        estimatedTime = (System.nanoTime() - startTime) / 1000000000;
    }

}
4

4 回答 4

3

也许你可以试试:

tx.postInvalidate();

其中 tx 是您的 TextView 对象

在这一行之后:

estimatedTime = (System.nanoTime() - startTime) / 1000000000;

但是您应该将“while”以下的所有代码放入线程中。例子:

new Thread(new Runnable() {

        @Override
        public void run() {
            //your code here
        }
    }).start();

更新:更新主线程,你可以使用这个:

YourActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //here set text into textView
    }
});
于 2013-09-06T11:08:35.243 回答
1

您不能在 UI 线程中将其作为阻塞来执行。您应该将其移至 AsyncTask。

于 2013-09-06T11:07:15.347 回答
1

最好使用 Handler 来延迟某些任务...

Handler handler = new Handler();
handler.postDelayed(new Runnable(){ 
    public void Run(){
        //Put your code here. Code will execute after 1000ms
    }
}, 1000);
于 2013-09-06T11:22:46.370 回答
1

使用另一个线程并调用 runOnUiThread 方法并传递该线程的对象。如下

Thread th=new Thread(new Runnable() {



        @Override
        public void run() {
            //put your code here
while (estimatedTime <= 100){
Thread.sleep(1000);
            System.out.println(""+estimatedTime);

        if(estimatedTime == 1){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing"); 

        }
        if(estimatedTime == 2){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Inatializing"); 

        }
        if(estimatedTime == 3){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing to install"); 

        }
        if(estimatedTime == 4){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installing"); 

        }
        if(estimatedTime == 5){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installed"); 

        }
        if(estimatedTime == 6){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Unzipping packages..."); 

        }


        estimatedTime = (System.nanoTime() - startTime) / 1000000000;
        }
    });

runOnUiThread(th);

于 2013-09-06T11:11:04.983 回答