Android 基础控件学习--Timer
【Android|Android 基础控件学习--Timer】
package net.blogjava.mobile;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class Main extends Activity
{
private ProgressBar progressBar;
private Handler handler = new Handler()
{public void handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
int currentProgress = progressBar.getProgress() + 2;
if (currentProgress > progressBar.getMax())
currentProgress = 0;
progressBar.setProgress(currentProgress);
break;
}
super.handleMessage(msg);
} };
private TimerTask timerTask = new TimerTask()
{public void run()
{
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
} };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
Timer timer = new Timer();
timer.schedule(timerTask, 0, 500);
}
}
大致思路,定义定时器及触发时的执行方法,方法中向handler发送消息,Handler接收到消息做响应动作。
推荐阅读
- Android|Android 基础控件学习--Menu点击跳转
- 浅识Flutter笔记|Android Studio里配置GenyMotion
- flutter|flutter实现瀑布流布局
- Pytorch|超最简单的安卓端深度学习模型部署教程——pytorch2onnx2pdinfer2android
- android|解决Android中使用ClickableSpan导致的内存泄漏
- Android|Kotlin 协程(二) -协程取消与超时
- autojs数据驱动界面和界面驱动数据
- stm32|stm32之继电器驱动
- 极客日报|小米首款汽车预计2024年量产;英伟达发布首款基于Hopper架构GPU;Java 18正式发布|极客头条