| 知乎专栏 |
目录
原理是使用 handler.postDelayed 延迟 Runnable 的运行时间
package cn.netkiller.okhttp;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class RunnableActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
this.update();
handler.postDelayed(this, 1000);// 1000 ms = 1s 间隔1秒
}
void update() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time.setText(dateFormat.format(new Date()));
}
};
private TextView time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_runnable);
time = (TextView) findViewById(R.id.time);
time.setText("Start...");
handler.postDelayed(runnable, 1000 * 5); // 5 秒后开始
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
}
//首先创建一个Handler对象
Handler handler=new Handler();
//然后创建一个Runnable对像
Runnable runnable=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//要做的事情,这里再次调用此Runnable对象,以实现每两秒实现一次的定时器操作
handler.postDelayed(this, 2000);
}
};
![]() | 提示 |
|---|---|
| 请使用单例模式,否则每次都拿到 handler 都是新对象,无法管理已经运行的对象。 |
//使用PostDelayed方法,调用此Runnable对象 handler.postDelayed(runnable, 2000); //关闭此定时器,可以这样操作 handler.removeCallbacks(runnable); //移除所有的消息 handler.removeCallbacksAndMessages(null);
下面用 ImageView 实现 Loading... 动画,实现方式,是吧图片 360 度旋转。
<ImageView
android:id="@+id/imageViewLoading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:visibility="invisible"
android:src="@android:drawable/ic_menu_rotate" />
ImageView imageViewLoading = view.findViewById(R.id.imageViewLoading);
Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = new Runnable() {
int count = 1;
@Override
public void run() {
handler.postDelayed(this, 50);
imageViewLoading.setRotation(count);
//每隔一段时间要重复执行的代码
// System.out.println("第" + (count++) + "次执行");
if (count == 360) {
handler.removeCallbacks(this);
imageViewLoading.setVisibility(View.INVISIBLE);
linearLayoutSpaek.setVisibility(View.VISIBLE);
}
}
};
handler.postDelayed(runnable, 100);
new Handler().postDelayed(() -> {
picture.browsePictureFolder();
}, 30000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
MainActivity.this.finish();
}
}, 1800);
Android 11
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
public void run() {
progressBar.setVisibility(View.INVISIBLE);
}
}, 3000);
循环执行
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
int count = 5;
@Override
public void run() {
handler.postDelayed(this, 1000);
runOnUiThread(() -> {
binding.textViewAdSkip.setText(String.format("逃过 %d", count));
});
count = count - 1;
if (count == 0) {
handler.removeCallbacks(this);
binding.frameLayoutAd.setVisibility(View.INVISIBLE);
binding.frameLayoutFullscreen.setVisibility(View.VISIBLE);
}
}
}, 1000);