2011-01-06 23 views

答えて

13

Timerは、メソッドの固定期間実行に使用できます。ここで

はコードのサンプルです:

final long period = 0; 
new Timer().schedule(new TimerTask() { 
    @Override 
    public void run() { 
     // do your task here 
    } 
}, 0, period); 
+1

ねえブロ...おかげで.... – Nirav

10

上記このリンクはテストされ、正常に動作します。これは毎秒何らかのメソッドを呼び出すコードです。あなたは変更することができます1000(= 1秒)したい任意の時間に(例えば3秒= 3000)

public class myActivity extends Activity { 

private Timer myTimer; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    myTimer = new Timer(); 
    myTimer.schedule(new TimerTask() {   
     @Override 
     public void run() { 
      TimerMethod(); 
     } 

    }, 0, 1000); 
} 

private void TimerMethod() 
{ 
    //This method is called directly by the timer 
    //and runs in the same thread as the timer. 

    //We call the method that will work with the UI 
    //through the runOnUiThread method. 
    this.runOnUiThread(Timer_Tick); 
} 


private Runnable Timer_Tick = new Runnable() { 
    public void run() { 

    //This method runs in the same thread as the UI.    

    //Do something to the UI thread here 

    } 
}; 
} 
+0

感謝。私のために働く。 – zwarrior

関連する問題