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

Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼

 更新時(shí)間:2017年08月01日 08:29:05   作者:中志融一  
本篇文章主要介紹了Android實(shí)現(xiàn)定時(shí)器Timer的停止和重啟實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文介紹了Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼,分享給大家,具體如下:

7月份做了一個(gè)項(xiàng)目,利用自定義控件呈現(xiàn)一幅動(dòng)畫(huà),當(dāng)時(shí)使用定時(shí)器來(lái)控制時(shí)間,但是當(dāng)停止開(kāi)啟時(shí)總是出現(xiàn)問(wèn)題。一直在尋找合理的方法解決這個(gè)問(wèn)題,一直沒(méi)有找到,最近終于找到了合理的方法來(lái)解決這個(gè)問(wèn)題。

大家如何查詢有關(guān)資料,一定知道timer,timertask取消的方式是采用Timer.cancel()和mTimerTask.cancel(),可是大家發(fā)現(xiàn)這種發(fā)式取消后,再次開(kāi)始timer時(shí),會(huì)報(bào)錯(cuò)

 FATAL EXCEPTION: main
         Process: com.example.zhongzhi.gate_control_scheme, PID: 2472
         java.lang.IllegalStateException: Timer already cancelled.
           at java.util.Timer.sched(Timer.java:397)
           at java.util.Timer.schedule(Timer.java:248)
           at com.example.zhongzhi.gate_control_scheme.MainActivity.onClick(MainActivity.java:401)
           at android.view.View.performClick(View.java:5637)
           at android.view.View$PerformClick.run(View.java:22429)
           at android.os.Handler.handleCallback(Handler.java:751)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:154)
           at android.app.ActivityThread.main(ActivityThread.java:6119)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

 這個(gè)問(wèn)題的解決采用cancle(),取消timer后,還需要清空timer。合理的代碼應(yīng)該是這樣的:

mTimer.cancel();
mTimer = null;
mTimerTask.cancel();
mTimerTask = null;

關(guān)鍵的問(wèn)題解決完了,下面給出我的案例代碼Mainactivity.Java:

public class MainActivity extends AppCompatActivity {

  private static String TAG = "TimerDemo";
  private TextView mTextView = null;
  private Button mButton_start = null;
  private Button mButton_pause = null;
  private Timer mTimer = null;
  private TimerTask mTimerTask = null;
  private Handler mHandler = null;
  private static int count = 0;
  private boolean isPause = false;
  private boolean isStop = true;
  private static int delay = 1000; //1s
  private static int period = 1000; //1s
  private static final int UPDATE_TEXTVIEW = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView)findViewById(R.id.mytextview);
    mButton_start = (Button)findViewById(R.id.mybutton_start);
    mButton_pause = (Button)findViewById(R.id.mybutton_pause);


    mButton_start.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        if (isStop) {
          Log.i(TAG, "Start");
        } else {
          Log.i(TAG, "Stop");
        }

        isStop = !isStop;

        if (!isStop) {
          startTimer();
        }else {
          stopTimer();
        }

        if (isStop) {
          mButton_start.setText(R.string.start);
        } else {
          mButton_start.setText(R.string.stop);
        }
      }
    });

    mButton_pause.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        if (isPause) {
          Log.i(TAG, "Resume");
        } else {
          Log.i(TAG, "Pause");
        }

        isPause = !isPause;

        if (isPause) {
          mButton_pause.setText(R.string.resume);
        } else {
          mButton_pause.setText(R.string.pause);
        }
      }
    });

    mHandler = new Handler(){
      @Override
      public void handleMessage(Message msg) {
        switch (msg.what) {
          case UPDATE_TEXTVIEW:
            updateTextView();
            break;
          default:
            break;
        }
      }
    };
  }

  private void updateTextView(){
    mTextView.setText(String.valueOf(count));
  }

  private void startTimer(){
    if (mTimer == null) {
      mTimer = new Timer();
    }

    if (mTimerTask == null) {
      mTimerTask = new TimerTask() {
        @Override
        public void run() {
          Log.i(TAG, "count: "+String.valueOf(count));
          sendMessage(UPDATE_TEXTVIEW);

          do {
            try {
              Log.i(TAG, "sleep(1000)...");
              Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
          } while (isPause);

          count ++;
        }
      };
    }

    if(mTimer != null && mTimerTask != null )
      mTimer.schedule(mTimerTask, delay, period);

  }

  private void stopTimer(){
    if (mTimer != null) {
      mTimer.cancel();
      mTimer = null;
    }
    if (mTimerTask != null) {
      mTimerTask.cancel();
      mTimerTask = null;
    }
    count = 0;
  }

  public void sendMessage(int id){
    if (mHandler != null) {
      Message message = Message.obtain(mHandler, id);
      mHandler.sendMessage(message);
    }
  }
}

xml部分代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <TextView
    android:id="@+id/mytextview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/number" />

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
      android:id="@+id/mybutton_start"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/start" />

    <Button
      android:id="@+id/mybutton_pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/pause" />
  </LinearLayout>
</LinearLayout>

string部分代碼:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">TimerDemo</string>
  <string name="number">0</string>
  <string name="start">start</string>
  <string name="stop">stop</string>
  <string name="pause">pause</string>
  <string name="resume">resume</string>
</resources>

上面就是我的源代碼,如果大家有什么問(wèn)題可以留言進(jìn)行探討。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • AndroidStudio 如何使用aar詳解

    AndroidStudio 如何使用aar詳解

    本文主要介紹AndroidStudio 如何使用aar的資料,這里整理了詳細(xì)的資料,幫助大家學(xué)習(xí)理解此部分的知識(shí),有需要的小伙伴可以參考下
    2016-09-09
  • Android編程判斷是否連接網(wǎng)絡(luò)的方法【W(wǎng)iFi及3G判斷】

    Android編程判斷是否連接網(wǎng)絡(luò)的方法【W(wǎng)iFi及3G判斷】

    這篇文章主要介紹了Android編程判斷是否連接網(wǎng)絡(luò)的方法,結(jié)合實(shí)例形式分析了Android針對(duì)WiFi及3G網(wǎng)絡(luò)連接的判斷方法,需要的朋友可以參考下
    2017-02-02
  • Android Parcleable接口的調(diào)用源碼層分析

    Android Parcleable接口的調(diào)用源碼層分析

    這篇文章主要給大家介紹了關(guān)于利用Kotlin如何實(shí)現(xiàn)Android開(kāi)發(fā)中的Parcelable的相關(guān)資料,并且給大家介紹了關(guān)于Android Parcleable源碼層問(wèn)題,需要的朋友可以參考下
    2022-12-12
  • 詳解Android進(jìn)程和線程

    詳解Android進(jìn)程和線程

    這篇文章主要為大家詳細(xì)介紹了Android進(jìn)程和Android線程兩個(gè)概念
    2016-06-06
  • Android開(kāi)發(fā)實(shí)現(xiàn)圖片平移、縮放、倒影及旋轉(zhuǎn)功能的方法

    Android開(kāi)發(fā)實(shí)現(xiàn)圖片平移、縮放、倒影及旋轉(zhuǎn)功能的方法

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片平移、縮放、倒影及旋轉(zhuǎn)功能的方法,涉及Android針對(duì)圖片的讀取、寫(xiě)入、屬性設(shè)置及矩陣運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android ListView詳解

    Android ListView詳解

    listview控件在項(xiàng)目開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)用到,本文給大家分享android listview相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例

    Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • Android創(chuàng)建外部lib庫(kù)及自定義View的圖文教程

    Android創(chuàng)建外部lib庫(kù)及自定義View的圖文教程

    這篇文章主要給大家介紹了關(guān)于Android創(chuàng)建外部lib庫(kù)及自定義View的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例

    Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例

    本篇文章主要介紹了Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 圖解Eclipse在線安裝ADT插件過(guò)程

    圖解Eclipse在線安裝ADT插件過(guò)程

    這篇文章主要以圖解的方式為大家分享了Eclipse在線安裝ADT插件過(guò)程,需要的朋友可以參考下
    2015-12-12

最新評(píng)論