知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
原理是使用 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); } }