版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QianNiYouShouZuo/article/details/80243173
场景是一个公司之前的老项目里面有人使用了Handler.postDelayed(mRunnable,1000)做一个重复计时并且累加的功能,需求是每一秒都要跑一次这个Runnable,同时是有视频在播放的,Runnable里面是比较视频播放的进度和我跑了多少秒来比对,再确定要不要做一些什么操作,然后一直这样重复跑,但是这个界面会不定时会打开一个像素1px的Activity界面去做拍照的功能,在拍照界面有一行代码这么写的,而且是在主线程直接这么写的(很无语):
Thread.sleep(2000);
之前一直没怎么注意这一点,经过蛋疼的各种调试发现刚好是在调用这行代码的2000ms时间内,我的Handler.postDelayed(mRunnable,1000)失效了。于是把sleep的代码移除成其他的方式,就恢复正常了。
于是自己写了个demo再实际一下效果:
package com.wepon.sleep; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private final String LOG_TAG = MainActivity.class.getSimpleName(); private TextView mTvDisplay; private Handler mHandler = new Handler(); private Runnable mRunnable = new Runnable() { @Override public void run() { Log.d(LOG_TAG, System.currentTimeMillis() / 1000 + ""); mTvDisplay.setText(System.currentTimeMillis() / 1000 + ""); mHandler.postDelayed(this, 1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvDisplay = findViewById(R.id.tv_display); // sleep findViewById(R.id.bt_sleep).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); // 开始计数 findViewById(R.id.bt_start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mHandler.postDelayed(mRunnable, 1000); } }); } @Override protected void onDestroy() { mHandler.removeCallbacks(mRunnable); super.onDestroy(); } }
<?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" tools:context=".MainActivity"> <Button android:id="@+id/bt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="71dp" android:text="start" /> <Button android:id="@+id/bt_sleep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignStart="@+id/bt_start" android:layout_marginTop="160dp" android:text="sleep 2秒" /> <TextView android:id="@+id/tv_display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="43dp" android:layout_marginTop="25dp" android:text="0" android:textSize="18sp" /> </RelativeLayout>
跑这个demo,先 start 会进行 1 秒打印一次时间点,然后点 sleep 2秒就会中断handler.postDelay()2秒了。如下:
希望踩这个坑的朋友能看到。。。还有就是不要在主线程去sleep,不要在主线程去sleep,不要在主线程去sleep!!