Android定時(shí)器實(shí)現(xiàn)定時(shí)執(zhí)行、重復(fù)執(zhí)行、定時(shí)重復(fù)執(zhí)行、定次數(shù)執(zhí)行的多種方式

作用:
1、定時(shí)執(zhí)行某種功能
2、重復(fù)執(zhí)行、定時(shí)重復(fù)執(zhí)行、定次數(shù)執(zhí)行某種功能
類別:
1、 Thread(new Runnable)
2、Thread()
3、Timer
4、Handler
·····
代碼如下:
1、布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/show_time"
android:text="請(qǐng)選擇一種啟動(dòng)方式"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/timer_1"
android:textAllCaps="false"
android:text="定時(shí)方式1(Thread(new Runnable))"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/timer_2"
android:text="定時(shí)方式2(Thread()) "
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/timer_3"
android:text="定時(shí)方式3(Timer) "
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/timer_4"
android:text="定時(shí)方式4(Handler) "
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/clear"
android:text="計(jì)時(shí)器清零 "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="10dp"
android:text="方式3停止方式不同(因?yàn)門imer一旦被cancel之后就廢了,只有重新構(gòu)造一個(gè))\n停止:1、開啟3 2、點(diǎn)擊停止 3、再點(diǎn)擊方式3"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2、實(shí)現(xiàn)定時(shí)功能
package com.example.leixiansheng.mytimers;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button timer_1, timer_2, timer_3, timer_4,clear, showTime;
private Timer timer;
private TimerTask timerTask;
private int num = 0; //計(jì)數(shù)值
private boolean flog = true; //是否停止計(jì)時(shí)
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
showTime.setText("點(diǎn)擊我停止計(jì)時(shí): " + msg.what);
}
};
//handler 方式定時(shí)循環(huán)
private Handler handlerTimer = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (flog) {
handlerTimer.sendEmptyMessageDelayed(num++, 1000);
}
showTime.setText("點(diǎn)擊我停止計(jì)時(shí): " + msg.what);
if(flog == false) {
flog = true;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer_1 = (Button) findViewById(R.id.timer_1);
timer_2 = (Button) findViewById(R.id.timer_2);
timer_3 = (Button) findViewById(R.id.timer_3);
timer_4 = (Button) findViewById(R.id.timer_4);
clear = (Button) findViewById(R.id.clear);
showTime = (Button) findViewById(R.id.show_time);
timer_1.setOnClickListener(this);
timer_2.setOnClickListener(this);
timer_3.setOnClickListener(this);
timer_4.setOnClickListener(this);
clear.setOnClickListener(this);
showTime.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.timer_1:
method_1();
break;
case R.id.timer_2:
method_2();
break;
case R.id.timer_3:
method_3();
break;
case R.id.timer_4:
method_4();
break;
case R.id.clear:
num = 0;
showTime.setText("請(qǐng)選擇一種啟動(dòng)方式");
break;
case R.id.show_time:
flog = false;
break;
}
}
private void method_4() {
new Thread() {
@Override
public void run() {
super.run();
handlerTimer.sendEmptyMessage(num++);
}
}.start();
}
private void method_3() {
//Timer一旦被cancel之后就廢了,只有重新構(gòu)造一個(gè)。
if (flog == true) {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(num++);
}
};
/**
* 第一個(gè)參數(shù):任務(wù)
* 第二個(gè)參數(shù):初始啟動(dòng)等待時(shí)間
* 第三個(gè)參數(shù):時(shí)間間隔
*/
timer.schedule(timerTask, 0, 1000);
} else {
timer.cancel();
// 一定設(shè)置為null,否則定時(shí)器不會(huì)被回收
timer = null;
flog = true;
}
//Timer 暫停重啟有問題(待改進(jìn))
//Timer一旦被cancel之后就廢了,只有重新構(gòu)造一個(gè)。
// if (flog == true) {
// timerTask = new TimerTask() {
// @Override
// public void run() {
// handler.sendEmptyMessage(num++);
// }
// };
// /**
// * 第一個(gè)參數(shù):任務(wù)
// * 第二個(gè)參數(shù):初始啟動(dòng)等待時(shí)間
// * 第三個(gè)參數(shù):時(shí)間間隔
// */
// timer.schedule(timerTask, 0, 1000);
// } else {
// timer.cancel();
// flog = true;
// }
}
private void method_2() {
//new Thread().start();
new Thread() {
@Override
public void run() {
super.run();
while (flog) {
handler.sendEmptyMessage(num++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
flog = true;
}
private void method_1() {
// new Thread(new Runnable).start();
new Thread(new Runnable() {
@Override
public void run() {
while (flog) {
handler.sendEmptyMessage(num++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
flog = true;
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼
- android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼
- Android單一實(shí)例全局可調(diào)用網(wǎng)絡(luò)加載彈窗
- Android帶刷新時(shí)間顯示的PullToRefresh上下拉刷新
- Android中LayoutInflater.inflater()的正確打開方式
- Android加載loading對(duì)話框的功能及實(shí)例代碼(不退出沉浸式效果)
- Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件
- Android仿微信標(biāo)簽功能
- Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁面的方法
- Android中buildToolVersion與CompileSdkVersion的區(qū)別
相關(guān)文章
Android Studio 全屏沉浸式透明狀態(tài)欄效果的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio 全屏沉浸式透明狀態(tài)欄效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Flutter實(shí)現(xiàn)仿京東商品詳情底部操作欄
這篇文章主要為大家詳細(xì)介紹了Flutter如何仿京東實(shí)現(xiàn)商品詳情底部操作欄,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
Android SQLite數(shù)據(jù)庫進(jìn)行查詢優(yōu)化的方法
這篇文章主要給大家介紹了關(guān)于Android SQLite數(shù)據(jù)庫進(jìn)行查詢優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
解決Kotlin 類在實(shí)現(xiàn)多個(gè)接口,覆寫多個(gè)接口中相同方法沖突的問題
這篇文章主要介紹了解決Kotlin 類在實(shí)現(xiàn)多個(gè)接口,覆寫多個(gè)接口中相同方法沖突的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android 四種動(dòng)畫效果的調(diào)用實(shí)現(xiàn)代碼
在這里, 我將每種動(dòng)畫分別應(yīng)用于四個(gè)按鈕為例,需要的朋友可以參考下2013-01-01

