2012-02-25 130 views
4

日誌 「你好」 只出現一次,敬酒沒有出現在所有..Android - 如何使用ScheduledExecutorService每10秒執行一次吐司?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ScheduledExecutorService scheduler = 
      Executors.newSingleThreadScheduledExecutor(); 
    scheduler.scheduleAtFixedRate(new Runnable() { 

     public void run() { 
      Log.i("hello", "world"); 
      Toast.makeText(getApplicationContext(), "It works", Toast.LENGTH_SHORT).show(); 
      // TODO Auto-generated method stub 

     } 
    }, 10, 10, TimeUnit.SECONDS); 

} 

回答

8

嘗試

 scheduler.scheduleAtFixedRate(new Runnable() { 

     public void run() { 
      Log.i("hello", "world"); 
      runOnUiThread(new Runnable() { 
       public void run() { 
        Toast.makeText(getApplicationContext(), "It works", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

     } 
    }, 10, 10, TimeUnit.SECONDS); 
+1

日Thnx兄弟,它的工作原理:) – SaberTrika 2012-02-25 10:06:44