Android中使用定時(shí)器的三種方法
本文實(shí)例為大家分享了Android中使用定時(shí)器的三種方法,供大家參考,具體內(nèi)容如下
圖示:

因?yàn)槎急容^簡單,所以就直接貼代碼(慮去再次點(diǎn)擊停止的操作),有個(gè)全局的Handler負(fù)責(zé)接收消息更新UI
第一種方法:Thread.sleep();方法
Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
}
第二種方法:Handler的postDelay()方法
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (isStart2) {
mHandler.sendEmptyMessage(0);
mHandler.postDelayed(this, 1000);
}
}
};
mHandler.postDelayed(runnable, 1000);
}
第三種:Timer和TimerTask
private Timer timer = new Timer();
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
};
timer.schedule(timerTask, 1000, 1000);
總的來說第三種方法最方便,不易出錯(cuò),第二種容易忘記添加出發(fā)事件.
貼一下完整代碼:
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.brioal.timertest.MainActivity"> <TextView android:id="@+id/main_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="Hello World!" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_tv" android:layout_marginTop="100dp" android:gravity="center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:onClick="Method1" android:text="方法1:Thread" android:textAllCaps="false" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:onClick="Method2" android:text="方法2:Handler" android:textAllCaps="false" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:onClick="Method3" android:text="方法3:Task" android:textAllCaps="false" /> </LinearLayout> </RelativeLayout>
MainActivity
package com.brioal.timertest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView mTv;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//收到消息后顯示當(dāng)前時(shí)間
long current = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String time = dateFormat.format(current);
mTv.setText(time);
}
};
private Timer timer = new Timer();
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
}
};
private Thread thread1;
private boolean isStart1 = false;
private boolean isStart2 = false;
private boolean isStart3 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.main_tv);
}
//Thread方法
public void Method1(View view) {
Runnable runnable = new Runnable() {
@Override
public void run() {
while (isStart1) {
mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
if (isStart1) {
isStart1 = false;
} else {
isStart1 = true;
thread1 = new Thread(runnable);
thread1.start();
}
}
public void Method2(View view) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (isStart2) {
mHandler.sendEmptyMessage(0);
mHandler.postDelayed(this, 1000);
}
}
};
if (isStart2) {
isStart2 = false;
} else {
mHandler.postDelayed(runnable, 1000);
isStart2 = true;
}
}
public void Method3(View view) {
if (isStart3) {
timer.cancel();
isStart3 = false;
} else {
timer.schedule(timerTask, 1000, 1000);
isStart3 = true;
}
}
}
總結(jié)完了,完整Github地址:TimerTest
更多內(nèi)容請點(diǎn)擊專題《java定時(shí)功能》進(jìn)行學(xué)習(xí)。
以上就是定時(shí)器使用方法的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能
- Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
- 詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法
- Android 定時(shí)器實(shí)現(xiàn)圖片的變換
- Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)定時(shí)器的五種方法實(shí)例詳解
- Android 實(shí)現(xiàn)定時(shí)器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例
- Android定時(shí)器實(shí)現(xiàn)的幾種方式整理及removeCallbacks失效問題解決
- 基于Android中實(shí)現(xiàn)定時(shí)器的3種解決方法
- Android定時(shí)器實(shí)現(xiàn)定時(shí)執(zhí)行、重復(fù)執(zhí)行、定時(shí)重復(fù)執(zhí)行、定次數(shù)執(zhí)行的多種方式
相關(guān)文章
安卓應(yīng)用開發(fā)通過java調(diào)用c++ jni的圖文使用方法
這篇文章主要介紹了2013-11-11
Android 使用Zbar實(shí)現(xiàn)掃一掃功能
這篇文章主要介紹了Android 使用Zbar實(shí)現(xiàn)掃一掃功能,本文用的是Zbar實(shí)現(xiàn)掃一掃,因?yàn)楦鶕?jù)本人對兩個(gè)庫的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實(shí)現(xiàn)方式也簡單,需要的朋友可以參考下2023-03-03
Android開發(fā)實(shí)現(xiàn)的幾何圖形工具類GeometryUtil完整實(shí)例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的幾何圖形工具類GeometryUtil,涉及Android坐標(biāo)圖形數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android使用Flutter實(shí)現(xiàn)錄音插件
這篇文章主要介紹了基于flutter實(shí)現(xiàn)錄音功能,介紹了如何錄音,如何把文件存放到本地,這些都是我們平常使用這個(gè)功能會遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-08-08
Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的方法
這篇文章主要介紹了Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的方法,較為詳細(xì)的分析了Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
Flutter輸入框TextField屬性及監(jiān)聽事件介紹
這篇文章主要介紹了Flutter輸入框TextField屬性及監(jiān)聽事件介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-11-11
android 把float轉(zhuǎn)換成Int的實(shí)例講解
今天小編就為大家分享一篇android 把float轉(zhuǎn)換成Int的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android自定義view實(shí)現(xiàn)太極效果實(shí)例代碼
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)太極效果實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

