2010-09-29 33 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」方法,您可能會看到一些奇怪的時間。

相關問題