Android Handler循环定时器

初次写博客,不废话,记载下来自己用的一些小技巧,

功能1:延迟触发特定功能;

        2:可循环触发;

直接上代码

public void recordtimetext(){
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message message=handler.obtainMessage(0);
            message.sendToTarget();
        }
    };
    if (recordtexttime){
    new Thread(runnable).start();
    }
}
 
  
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
              //在这里做想要做的事情
 
  
            recordtimetext();

              break;
}};

每次调用以下方法可实现循环工作;
recordtimetext();

你可能感兴趣的:(Android Handler循环定时器)