android studio定时器
1.超时
CountDownTimer第一个参数超时时间,第二个参数多久执行一次onTick(), 到达设定的超时时间执行onFinsh(),cancel取消超时计数,start重新开始(从零开始)。
private CountDownTimer afterRunTimer=new CountDownTimer(2000,2000) { @Override public void onTick(long l) { LogUtil.d("TAG", "onTick: afterRunTimer"); } @Override public void onFinish() { afterRunTimer.cancel(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { LogUtil.d("TAG", "onFinish: afterRunTimer readSN"); readSN(); } }); } };
2.
handler.postDelayed(myRunnable,1000);
private Handler handler=new Handler(); private Runnable myRunnable=new Runnable() { @Override public void run() { Log.d("TAG","myRunnable"); //间隔时间循环执行 //handler.postDelayed(myRunnable,1000); } };