最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)時(shí)使用定時(shí)器來控制時(shí)間,但是當(dāng)停止開啟時(shí)總是出現(xiàn)問題。一直在尋找合理的方法解決這個(gè)問題,一直沒有找到,最近終于找到了合理的方法來解決這個(gè)問題。

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

 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è)問題的解決采用cancle(),取消timer后,還需要清空timer。合理的代碼應(yīng)該是這樣的:

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

關(guā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>

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

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

相關(guān)文章

  • Android?Choreographer源碼詳細(xì)分析

    Android?Choreographer源碼詳細(xì)分析

    Choreographer的作用主要是配合Vsync,給上層App的渲染提供一個(gè)穩(wěn)定的Message處理的時(shí)機(jī),也就是Vsync到來的時(shí)候,系統(tǒng)通過對Vsync信號周期的調(diào)整,來控制每一幀繪制操作的時(shí)機(jī)
    2022-08-08
  • Android通過Path實(shí)現(xiàn)搜索按鈕和時(shí)鐘復(fù)雜效果

    Android通過Path實(shí)現(xiàn)搜索按鈕和時(shí)鐘復(fù)雜效果

    這篇文章主要為大家詳細(xì)介紹了Android通過Path實(shí)現(xiàn)搜索按鈕和時(shí)鐘復(fù)雜效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 微信公眾平臺開發(fā)入門教程(圖文詳解)

    微信公眾平臺開發(fā)入門教程(圖文詳解)

    由于微信的大熱,為了更好的方便使用微信的用戶查詢一些信息,這篇文章是入門級的微信公眾平臺開發(fā)教程,需要的朋友可以參考下
    2013-09-09
  • Android實(shí)現(xiàn)炫酷播放效果

    Android實(shí)現(xiàn)炫酷播放效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)炫酷播放效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 解決Android Studio 出現(xiàn)“Cannot resolve symbol” 的問題

    解決Android Studio 出現(xiàn)“Cannot resolve symbo

    今天在調(diào)試的時(shí)候,Android Studio報(bào)了一個(gè)莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因?yàn)檫@東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下
    2023-03-03
  • Android Fragment監(jiān)聽返回鍵的一種合理方式

    Android Fragment監(jiān)聽返回鍵的一種合理方式

    這篇文章主要給大家介紹了關(guān)于Android Fragment監(jiān)聽返回鍵的一種合理方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • OpenGL Shader實(shí)例分析(7)雪花飄落效果

    OpenGL Shader實(shí)例分析(7)雪花飄落效果

    這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實(shí)例分析第7篇,實(shí)現(xiàn)雪花飄落效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android 進(jìn)度條按鈕ProgressButton的實(shí)現(xiàn)代碼

    Android 進(jìn)度條按鈕ProgressButton的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android 進(jìn)度條按鈕實(shí)現(xiàn)(ProgressButton)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-10-10
  • android控制密碼顯示與隱藏的方法

    android控制密碼顯示與隱藏的方法

    這篇文章主要為大家詳細(xì)介紹了android控制密碼顯示與隱藏的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android實(shí)現(xiàn)上傳圖片功能

    Android實(shí)現(xiàn)上傳圖片功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)上傳圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論

玛多县| 富蕴县| 电白县| 广饶县| 寿宁县| 南靖县| 镇平县| 乐平市| 长寿区| 杭锦后旗| 临海市| 黎川县| 顺平县| 长宁县| 宝坻区| 永靖县| 鲁甸县| 商都县| 乌拉特中旗| 阿拉尔市| 马尔康县| 甘谷县| 兴隆县| 疏勒县| 三明市| 浙江省| 三亚市| 大渡口区| 淮滨县| 美姑县| 成武县| 米易县| 当涂县| 健康| 化州市| 鹤壁市| 龙游县| 廊坊市| 景东| 增城市| 梁山县|