首页 > 其他分享 >怎么定时刷新界面(转)

怎么定时刷新界面(转)

时间:2023-02-27 13:32:01浏览次数:36  
标签:界面 void timer handler 刷新 msg new 定时 public


在做Android客户端软件的时候经常需要刷新某区块内容,比如微博客户端就需要定期检测是否有新发布的微博内容,如果有新微博客户端就显示出来。Android里可以选用两种方式来实现此功能。

方式一、使用Timer(定时器)和TimerTask实现

示例代码:

public class MainActivity extends Activity {
private TextView msg;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
update();
break;
}
super.handleMessage(msg);
}
void update() {
//刷新msg的内容
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msg = (TextView) findViewById(R.id.txtMsg);
msg.setText("你好啊!");
timer.schedule(task, 1000 * 40, 1000 * 30); //启动timer
}
@Override
protected void onDestroy() {
if (timer != null) {// 停止timer
timer.cancel();
timer = null;
}
super.onDestroy();
}
}

方式二、使用Runnable和Handler

示例代码

public class MainActivity extends Activity {
private TextView msg;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
this.update();
handler.postDelayed(this, 1000 * 120);// 间隔120秒
}
void update() {
//刷新msg的内容
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msg = (TextView) findViewById(R.id.txtMsg);
msg.setText("你好啊!");
handler.postDelayed(runnable, 1000 * 60);
}
@Override
protected void onDestroy() {
handler.removeCallbacks(runnable); //停止刷新
super.onDestroy();
}
}

第一种方式还适用于消息通知的方式实现更新,第二种方式通常是主动去检查是否需要刷新。对于定时刷新这种使用第二种方式更

标签:界面,void,timer,handler,刷新,msg,new,定时,public
From: https://blog.51cto.com/u_15070324/6088367

相关文章

  • CentOS 7关闭图形化界面
    CentOS7关闭图形化界面旋转的冬瓜皮于2021-12-1316:46:46发布7344收藏36分类专栏:linux文章标签:centos服务器linux版权华为云开发者联盟该内容已被华为云开发者......
  • CH58x嘀嗒定时器(SysTick)
    一、 SysTick是一个定时器,只是它放在了NVIC中,主要的目的是为了给操作系统提供一个硬件上的中断(号称滴答中断)。只要不把它在SysTick控制及状态寄存器中的使能位清除,就永不......
  • shell脚本定时任务转移项目日志
    1、之前同时项目部署在根目录,根目录磁盘空间40G,运行一年后日志占了18G的磁盘空间,根目录只有几个G的磁盘空间,现在写shell脚本定时转移日志文件到挂载的磁盘目录下2、编写s......
  • Vue2 里如何优雅的清除一个定时器
    绝大多数人清除定时器的方法<script>exportdefault{data(){return{timer:null}},mounted(){this.timer=setInterval(()=>{......
  • Oracle 创建、暂停、更改、删除 定时任务job
    --查询当前库中运行的job;ELECTt.*FROM dba_jobst             --创建一个定时任务job   declare      jobnumber;    BEGIN......
  • jmeter运行环境及界面初识
    1、安装jdk2、下载jmeter并启动jmeter.bat3、工具本身日志,java代码日志  4、添加线程组:线程数、循环次数5、取样器:不同协议不同取样器http、soap、jdbc、websocket......
  • stm32f407探索者开发板(二十二)——通用定时器基本原理讲解
    文章目录​​一、三种定时器的区别​​​​二、通用定时器特点​​​​2.1功能特点描述​​​​2.2计数器模式​​​​三、通用定时器工作过程​​​​四、附​​一、三种......
  • LQB04 定时器代码使用01,定时闪烁灯和定时框架。
    1、STC-ISP软件定时函数的生成,定时案例库的学习;2、定时器0,闪烁LED1;定时500ms,间隔定时器1,闪烁LED2,定时800ms,间隔。3、快速搭建框架。这里只讲解直接的使用,不讲解内部细节......
  • 基于BP神经网络的数字识别系统仿真,带GUI界面
    1.算法描述OCR(OpticalCharacterRecognition)即光学字符识别技术,是通过扫描仪把印刷体或手写体文稿扫描成图像,然后识别成相应的计算机可直接处理的字符。OCR是模式识别的一......
  • SpringBoot24 - 定时任务
    任务​ springboot整合第三方技术第二部分我们来说说任务系统,其实这里说的任务系统指的是定时任务。定时任务是企业级开发中必不可少的组成部分,诸如长周期业务数据的计算......