1. 程式人生 > >ScheduledExecutorService定時週期執行指定的任務

ScheduledExecutorService定時週期執行指定的任務

  1. public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,  
  2.             long initialDelay,  
  3.             long period,  
  4.             TimeUnit unit);  

command:執行執行緒
initialDelay:初始化延時
period:兩次開始執行最小間隔時間

unit:計時單位

scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new PagerViewScrollTaskRunable(), 1, 5,TimeUnit.SECONDS);

private Handler viewPagerHandler = new Handler(){//頁面檢視控制器
public void handleMessage(android.os.Message msg) {
vp_insert_banner.setCurrentItem(currentItem);
};
};

private class PagerViewScrollTaskRunable implements Runnable {//頁面檢視滾動工作執行緒類
public void run() {
synchronized (vp_insert_banner) {
currentItem = (currentItem + 1) % imageViews.size();
viewPagerHandler.obtainMessage().sendToTarget();
}
}
}