0
点赞
收藏
分享

微信扫一扫

Android 中简单计时器的实现方法(Handler和TimerTask)

凉夜lrs 2022-11-17 阅读 50

方法一、用handler线程创建计时器

计时器效果如下:

Android 中简单计时器的实现方法(Handler和TimerTask)_android


第一步:写xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击开始计时:"
android:id="@+id/btn"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/tv"
/>

</LinearLayout>

第二步:在Activity中,创建线程,具体讲解都在代码中注释了

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn=findViewById(R.id.btn);
tv=findViewById(R.id.tv);

btn.setOnClickListener(this);
}
//创建一个处理器对象
final Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String s= (String) msg.obj;
tv.setText(s);
}
};


@Override
public void onClick(View v) {
new Thread(){
@Override
public void run() {
int i=0;
super.run();
while (i<=9){
i++;
String s=""+i;
//发送给处理器去处理,用处理器handler,创建一个空消息对象
Message message=handler.obtainMessage();
//把值赋给message
message.obj=s;
//把消息发送给处理器
handler.sendMessage(message);
try {
//延时是一秒
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}

方法二、使用TimerTask,定义一个TimerTask处理handler事件,创建定时器

1、Timer类的概述

  1. Timer是Android直接启动定时器的类,TimerTask是一个子线程,方便处理一些比较复杂耗时的功能逻辑,经常与handler结合使用。
  2. 跟handler自身的实现的定时器相比,Timer可以做一些复杂的处理。例如,需要对有大量对象的list进行排序,在TimerTask中执行不会阻塞子线程,常常与handler结合使用,在处理完复杂耗时的操作后,通过handler来更新UI界面。

2、注意事项:

  1. 定时器需要在onDestory()方法中取消掉,否则可能发生崩溃。
  2. 用TimerTask定时进行某些操作的APP,即使退出了,TimerTask中依然会执行一会,但是不能长时间运行。
  3. 对于部分手机,如果你在TimerTask直接更新了UI线程是不会报错的,而且能正常运行;但是一定要注意,​​更新UI一定要在主线程中执行​​,否则排错时,不容易发现问题。而且这个东西特别耗电!一定要在不使用的时候关闭。

Android 中简单计时器的实现方法(Handler和TimerTask)_android_02


完整代码如下:

1.创建xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始倒计时"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="点火"
/>

</LinearLayout>

2.在Activity中,创建定时器

public class MainActivity extends AppCompatActivity{

Timer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn=findViewById(R.id.btn);
TextView tv=findViewById(R.id.tv);


//定义一个Handler
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what>0){
tv.setText("倒计时"+msg.what);
}else {
tv.setText("点火");
timer.cancel(); //关闭定时器
}
}
};

//点击按钮,开始倒计时
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//定义定时器
timer=new Timer();
timer.schedule(new TimerTask() {
int i=10;
@Override
public void run() {
//定义一个消息传过去
Message msg=new Message();
msg.what=i--;
handler.sendMessage(msg);
}
},0,1000); //延时0毫秒开始计时,每隔1秒计时一
//次
}
});
}
}

以上是简单的两种定时器的实现方法,仅供参考,有不善的地方,欢迎提出,加以更正,学习一直在路上!


举报

相关推荐

0 条评论