上述就是android开发分享Android常用定时器的实现方式的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
前言
android开发中,常用定时器实现方式有以下几种:
1、handler与sleep
2、handler与postdelayed
3、handler与timer
注:handler的主要作用就是用来处理接收到的信息;用handler消息传递机制是为了多个线程并发更新ui的同时,保证线程安全;
一、handler 与 sleep
public class handlerandsleep { // 1、定义一个handler类,用于处理接受到的message. handler handler = new handler(looper.getmainlooper()) { @override public void handlemessage(@nonnull message msg) { super.handlemessage(msg); // do something eg. log.d("handlerandsleep", "handlerandsleep"); } }; // 2、新建一个实现runnable接口的线程类 class mythread implements runnable { @override public void run() { while (true) { try { thread.sleep(10000); message message = new message(); message.what = 1; handler.sendmessage(message); } catch (exception e) { e.printstacktrace(); } } } } public handlerandsleep() { //3、在需要启动线程的地方加入下面语句,启动线程后,线程每10s发送一次消息 new thread(new mythread()).start(); } }
结果打印如下:
2022-05-30 11:30:42.320 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:30:52.324 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:31:02.326 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:31:12.335 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:31:22.338 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:31:32.339 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
2022-05-30 11:31:42.347 17864-17864/com.yh.study d/handlerandsleep: handlerandsleep
二、handler 与 postdelayed
public class handleandpostdelayed { handler handler = new handler(){ @override public boolean sendmessageattime(@nonnull message msg, long uptimemillis) { log.d("handleandpostdelayed", "handleandpostdelayed"); return super.sendmessageattime(msg, uptimemillis); } }; runnable runnable = new runnable() { @override public void run() { // do something handler.postdelayed(this, 2000); } }; handleandpostdelayed() { // 开启定时器 handler.postdelayed(runnable, 2000); // 停止定时器 // handler.removecallbacks(runnable); } }
结果打印如下:
2022-05-30 14:32:41.533 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
2022-05-30 14:32:43.534 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
2022-05-30 14:32:45.537 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
2022-05-30 14:32:47.539 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
2022-05-30 14:32:49.546 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
2022-05-30 14:32:51.550 24351-24351/com.yh.study d/handleandpostdelayed: handleandpostdelayed
三、handler 与 timer
public class handlerandtimer { private final timer timer = new timer(); private timertask task; handler handler = new handler() { @override public void handlemessage(@nonnull message msg) { // do something log.d("handlerandtimer", "handlerandtimer"); super.handlemessage(msg); } }; public handlerandtimer() { task = new timertask() { @override public void run() { message message = new message(); message.what = 1; handler.sendmessage(message); } }; //启动定时器 参数对应为 timertask 延迟时间 间隔时间 timer.schedule(task, 2000, 2000); } }
结果打印如下:
2022-05-30 14:43:40.922 25052-25052/com.yh.study d/handlerandtimer: handlerandtimer
2022-05-30 14:43:42.924 25052-25052/com.yh.study d/handlerandtimer: handlerandtimer
2022-05-30 14:43:44.925 25052-25052/com.yh.study d/handlerandtimer: handlerandtimer
2022-05-30 14:43:46.926 25052-25052/com.yh.study d/handlerandtimer: handlerandtimer
2022-05-30 14:43:48.927 25052-25052/com.yh.study d/handlerandtimer: handlerandtimer
在android上常用的定时器有两种,一种是java.util.timer,一种就是系统的alarmservice。
public class mainactivity extends appcompatactivity { handler handler = new handler() { @override public void handlemessage(@nonnull message msg) { tcount++; timertv.settext(tcount+""); super.handlemessage(msg); } }; timer timer = new timer(); int tcount = 0; int scount = 0; textview timertv; textview servicetv; string alarm_receiver_action = "com.yh.study.alarmreceiver"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); timertv = findviewbyid(r.id.timer_tv); servicetv = findviewbyid(r.id.service_tv); timer.schedule(new timertask() { @override public void run() { handler.sendemptymessage(0); } }, 2*1000, 5*1000); alarmreceiver alarmreceiver = new alarmreceiver(new handler() { @override public void handlemessage(@nonnull message msg) { scount++; servicetv.settext(scount+""); super.handlemessage(msg); } }); intentfilter intentfilter = new intentfilter(); intentfilter.addaction(alarm_receiver_action); registerreceiver(alarmreceiver, intentfilter); intent intent = new intent(alarm_receiver_action); pendingintent pendingintent = pendingintent.getbroadcast(mainactivity.this, 0, intent, 0); // alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.setexactandallowwhileidle(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime(), pendingintent); } }
public class alarmreceiver extends broadcastreceiver { handler handler; alarmreceiver(handler handler) { this.handler = handler; } @override public void onreceive(context context, intent intent) { log.d("onreceive", "onreceive"); handler.sendemptymessage(0); intent serviceintent = new intent(context, myservice.class); context.startservice(serviceintent); } }
public class myservice extends intentservice { public myservice() { super("myservice"); } @override protected void onhandleintent(@nullable intent intent) { alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); intent alarmintent = new intent("com.yh.study.alarmreceiver"); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, alarmintent, 0); alarmmanager.setexactandallowwhileidle(alarmmanager.rtc_wakeup, system.currenttimemillis()+5*1000, pendingintent); } }
以下结论来源其他博主
(小子在验证过程中,连接usb情况正常,拔掉usb后与结论不符,可能是小子哪里写的有问题)
在timer中,当连接usb线进行调试时,会发现一切工作正常,每5秒更新一次界面,即使是按下电源键,仍然会5秒触发一次。 当拔掉usb线,按下电源键关闭屏幕后,过一段时间再打开,发现定时器明显没有继续计数,停留在了关闭电源键时的数字。
在alarmservice中,拔掉usb线,按下电源键,过一点时间再次打开屏幕,发现定时器一直在计数。
测试机型小米8,上timer,下alarmservice
补充:采用handler与timer及timertask结合的方法
1. 定义定时器、定时器任务及handler句柄
private final timer timer = new timer(); private timertask task; handler handler = new handler() { @override public void handlemessage(message msg) { // todo auto-generated method stub // 要做的事情 super.handlemessage(msg); } };
2. 初始化计时器任务
task = new timertask() { @override public void run() { // todo auto-generated method stub message message = new message(); message.what = 1; handler.sendmessage(message); } };
3. 启动定时器
timer.schedule(task, 2000, 2000);
总结
到此这篇关于android常用定时器实现的文章就介绍到这了,更多相关android常用定时器内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/1236376.html