在项目使用 Handler.postDelayed()
延时执行任务,却发现无效,代码如下:
private Handler mHandler = new Handler();
boolean ret = mHandler.postDelayed(new Runnable() {
@Override
public void run() {
}
}, delay);
Log.d("Nat", "postDelayed:" + ret);
修改后的代码如下:
private Handler mHandler = new Handler(Looper.getMainLooper());
boolean ret = mHandler.postDelayed(new Runnable() {
@Override
public void run() {
}
}, delay);
Log.d("Nat", "postDelayed:" + ret);
总结:需要给Handler
设置一个主线程的 looper
。
网友评论