本文,介绍常用定时器实现方式:
1)Handler+Sleep方式
2)Handler+PostDelayed方式
3)Handler+Timer方式
Handler的主要作用就是用来处理接收到的信息,用Handler消息传递机制是为了多个线程 并发 更新U的同时,保证线程安全
1)Handler+Sleep方式
1.1)Handler+Sleep定义
public class HandlerAndSleep { public HandlerAndSleep() { } //定义Handler对象,用于处理接收Message Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(@NonNull Message msg){ super.handleMessage(msg); Log.d("HandlerAndSleep","HandlerAndSleep,时间:"+ ToolHelper.GetNowDate()); } }; //新建一个实现Runnable接口的线程类 class MyThread implements Runnable{ @Override public void run() { while (true){ try { Thread.sleep(1000); Message message = new Message(); message.what = 1; handler.sendMessage(message); } catch (InterruptedException e) { e.printStackTrace(); } } } } //开启 public void Start(){ new Thread(new MyThread()).start(); } //关闭(存在问题,无法使用) public void Stop(){ new Thread(new MyThread()).stop(); } }
1.2)Handler+Sleep调用
public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test);
//HandlerAndSleep实现定时器调用 HandlerAndSleep handlerAndSleep = new HandlerAndSleep(); handlerAndSleep.Start(); } }
1.3)Handler+Sleep结果
2)Handler+PostDelayed方式
2.1)Handler+PostDelayed定义
public class HandlerAndPostDelayed { public HandlerAndPostDelayed(){ } //定义Handler对象,用于处理接收Message Handler handler = new Handler(){ @Override public boolean sendMessageAtTime(@NonNull Message msg,long uptimeMillis){ Log.d("HandlerAndPostDelayed","HandlerAndPostDelayed,时间:"+ ToolHelper.GetNowDate()); return super.sendMessageAtTime(msg,uptimeMillis); } }; Runnable runnable = new Runnable() { @Override public void run() { handler.postDelayed(this,1000); } }; //开启定时器 public void Start(){ handler.postDelayed(runnable,1000); } //关闭定时器 public void Stop(){ handler.removeCallbacks(runnable); } }
2.2)Handler+PostDelayed调用
public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); //HandlerAndPostDelayed实现定时器调用 HandlerAndPostDelayed handlerAndPostDelayed = new HandlerAndPostDelayed(); handlerAndPostDelayed.Start();
}
}
2.3)Handler+PostDelayed结果
3)Handler+Timer方式
3.1)Handler+Timer定义
public class HandlerAndTimer { public HandlerAndTimer(){
} private final Timer timer = new Timer(); Handler handler = new Handler(){ @Override public void handleMessage(@NonNull Message msg){ Log.d("HandlerAndTimer","HandlerAndTimer,时间:"+ ToolHelper.GetNowDate()); super.handleMessage(msg); } }; TimerTask timerTask = new TimerTask() { @Override public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; public void Start(){ timer.schedule(timerTask,0,1000); } public void Stop(){ timer.cancel(); } }
3.2)Handler+Timer调用
public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); //HandlerAndTimer实现定时器调用 HandlerAndTimer handlerAndTimer = new HandlerAndTimer(); handlerAndTimer.Start(); } }
3.3)Handler+Timer结果
附加下获取当前时间方法
public class ToolHelper { public static String GetNowDate(){ //获取当前时间 Date date = new Date(System.currentTimeMillis()); //日期格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } }
参考来源:https://blog.csdn.net/Twan1234/article/details/125050488
标签:定时器,Message,void,学习,Handler,Override,new,Android,public From: https://www.cnblogs.com/xielong/p/17749860.html