我知道handler.post()方法,导致Runnable r被添加到消息队列中。 runnable将在连接此处理程序的线程上运行。
但我的问题是我在onCreate()方法中使用了runtimer(),在onCreate()方法完成后或者??
之后执行消息队列中的runnable public class Stopwatch extends Activity {
private int seconds;
private boolean Running;
private boolean wasrunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null){
seconds = savedInstanceState.getInt("seconds");
Running = savedInstanceState.getBoolean("running");
wasrunning = savedInstanceState.getBoolean("wasrunning");
}
runtimer();
}
public void start(View v)
{
Running=true;
wasrunning= true;
}
public void stop(View v)
{
wasrunning=false;
Running=false;
}
public void reset(View v){
seconds=0;
Running= false;
wasrunning=false;
}
void runtimer(){
final TextView tv = (TextView) findViewById(R.id.text);
final Handler h = new Handler();
h.post( new Runnable(){
@Override
public void run() {
int hour = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int sec = seconds % 60;
String time = String.format("%02d:%02d:%02d", hour, minutes, sec);
tv.setText(time);
if (Running) {
seconds++;
}
h.postDelayed(this,1000);
}
});
}
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("seconds",seconds);
savedInstanceState.putBoolean("Running",Running);
savedInstanceState.putBoolean("wasrunning",wasrunning);
}
}
答案 0 :(得分:0)
在onCreate()方法
完成后,是否执行消息队列中的runnable
在onCreate()
方法返回后的某个时间。究竟何时将取决于主应用程序线程的消息队列中的其他内容以及需要多长时间。