private Handler handler = new Handler(); private Runnable task = new Runnable() { public void run() { // TODO Auto-generated method stub handler.postDelayed(this,5*1000);//设置延迟时间,此处是5秒 //需要执行的代码 } };
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); handler.postDelayed(task,5000);//延迟调用 handler.post(task);//立即调用 }