2010-09-29 61 views
4

你好,我想做每一秒钟调用函数或做别的事情的apliacation。 我有这个代码不工作,你能告诉什么是错的?我如何使Android应用程序每X秒做一件事

public class App5_Thread extends Activity implements Runnable {  
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       Thread thread = new Thread(this);  
       thread.start(); 
     } 
     @Override      
     public void run() {     
       TextView tv1 = (TextView) findViewById(R.id.tv); 
       showTime(tv1);                 
       try { 
        Thread.sleep(1000); 
       }catch (Exception e) { 
        tv1.setText(e.toString()); 
       }    
     } 
     public void showTime(TextView tv1){     
      String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; 
      Calendar cal = Calendar.getInstance(); 
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 
      tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis());      
     }   

}

+0

这是什么不工作?它没有给出预期的输出?例外?你还没有想到的其他东西? – eldarerathis 2010-09-29 14:40:33

+0

你不应该循环,而应该在自己的线程上使用计时器。线程在每次循环时都会响应该接口。这将使应用程序不会受到您的等待声明的阻碍。 – JustinKaz 2010-09-29 15:05:22

+1

'公共类App5_Thread扩展活动实现Runnable'这是一个非常糟糕的主意 – Falmarri 2010-09-29 20:21:25

回答

0

我有以下

更改代码有个个3种方式工作(显示日志输出)。

但是TextView没有更新。为什么?

package cz.cvut.fel.android; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class App5_RepeatedAction extends Activity { 

    static TextView tv1;  
    static class MyThread implements Runnable { 
     public void run() { 
      boolean end = false; 
      while (!end) { 
       String txt = "Vlakno id:" + Thread.currentThread().getId()+" THREAD"; 
       Log.v("MyActivity", txt); 
       //tv1.setText(txt); 
       showTime(tv1); 
       try { 
        Thread.sleep(10000); 
       } catch (InterruptedException ex) { 
        System.err.println(ex.toString()); 
       } 
      } 
     } 
    }  
    static void showTime(TextView tv1){     
     String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; 
     Calendar cal = Calendar.getInstance(); 
     SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 
     tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis());      
    } 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv1 = (TextView) findViewById(R.id.tv); 
     //----------ScheduledExecutorService 
     ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); 
     exec.scheduleAtFixedRate(new Runnable() { 
      @Override 
      public void run() { 
       String txt = "ScheduledExecutorService"; 
       Log.v("MyActivity", txt); 
       //tv1.setText(txt); 
       showTime(tv1); 
      } 
     }, 0, 5, TimeUnit.SECONDS); 

     //----------TIMER 
     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       String txt = "Timer"; 
       Log.v("MyActivity", txt); 
       //tv1.setText(txt); 
       showTime(tv1); 
      } 
     }, 0, 1000); 
     //-----------THREAD 
     try { 
      boolean quit = false;   
      Thread t = new Thread(new MyThread()); 
      //t.setDaemon(true); 
      t.start();    
     } catch (Exception e) { 
      System.err.println(e.toString()); 
     } 
    } 
} 
+0

因为更新TextView的行被注释掉了......? – Jonathan 2011-05-19 23:56:31

+0

我使用showTime方法而不是更新textView – Tom 2011-07-27 19:03:20

+0

这是错的,男人。 – 2012-08-01 20:55:45

6

您不必在运行一个循环()函数,试图改变这样的:

@Override      
    public void run() { 
      TextView tv1 = (TextView) findViewById(R.id.tv); 
      while(true){ 
       showTime(tv1);                 
       try { 
        Thread.sleep(1000); 
       }catch (Exception e) { 
        tv1.setText(e.toString()); 
       }   
      } 
    }    

要小心,它会永远跑不过。你也应该使用一个处理程序从另一个线程上执行的UI的变化,它可以通过下面的例子来完成:

Handler mHandler = new Handler(); 
    @Override      
    public void run() { 
     final TextView tv1 = (TextView) findViewById(R.id.tv); 
     while(true){                
      try { 
       mHandler.post(new Runnable(){ 
        @Override 
        public void run() { 
         showTime(tv1); 
        } 
       ); 
       Thread.sleep(1000); 
      }catch (Exception e) { 
       //tv1.setText(e.toString()); 
      }   
     } 
    }  
+0

这将保持处理器和其他资源的繁忙。我建议使用'Handler'。 – 2013-10-14 15:43:31

3

就像我之前说的,你一定要修改UI线程你的TextView(创建该组件的线程)。

为了做到这一点使用一个处理程序,像这样: (不循环在你的线程,只是发布消息处理程序)

private TextView tv1; 

Handler tick_Handler = new Handler(); 
MyThread tick_thread = new MyThread(); 

private class MyThread implements Runnable { 
    public void run() { 
      String txt = "Vlakno id:" + Thread.currentThread().getId()+" THREAD"; 
      Log.v("MyActivity", txt); 
      //tv1.setText(txt); 
      showTime(tv1); 
      tick_Handler.postDelayed(tick_thread, 1000); 
    } 
}  

String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; 
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 

private void showTime(TextView tv){  
Calendar cal = Calendar.getInstance(); 
tv.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis()); 
} 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tv1 = (TextView) findViewById(R.id.tv); 

    tick_Handler.post(tick_thread); 
} 

顺便说一句,如果你想有一个准确的计时器,你应该每300毫秒打勾。如果每秒执行一次“showtime”方法,您可能会看到一些奇怪的时间。

相关问题