if (autoSwitchpicTask==null){ autoSwitchpicTask = new AutoSwitchPicTask(); autoSwitchpicTask.start(); }else { autoSwitchpicTask.start(); } } class AutoSwitchPicTask extends Handler implements Runnable { /** * 开启任务 */ public void start() { stop(); postDelayed(this, 3000); } /** * 关闭任务 */ public void stop() { removeCallbacks(this); } @Override public void run() { int position = viewPager.getCurrentItem(); System.out.println("position "+position); if (position != viewPager.getAdapter().getCount() - 1) { viewPager.setCurrentItem(++position); } else { viewPager.setCurrentItem(0); } postDelayed(this, 3000); } }