欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年12月11日 11:31:10   作者:Mr_Leixiansheng  
今天小編就為大家分享一篇關(guān)于Android定時(shí)器實(shí)現(xiàn)定時(shí)執(zhí)行、重復(fù)執(zhí)行、定時(shí)重復(fù)執(zhí)行、定次數(shù)執(zhí)行的多種方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

作用:

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、開(kāi)啟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 暫停重啟有問(wèn)題(待改進(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)鏈接

相關(guān)文章

最新評(píng)論