我正在使用 System.NanoTime 来跟踪经过的时间,但它不会在 UI 上更新。以下是我的代码:
然而,我在方法中做所有事情,onCreate()
我采用的方法可能并不可靠,但这就是我想要更多想法的地方。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = (System.nanoTime() - startTime) / 1000000000;
System.out.println(""+estimatedTime);
while (estimatedTime <= 100){
System.out.println(""+estimatedTime);
if(estimatedTime == 1){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Preparing");
}
if(estimatedTime == 2){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Inatializing");
}
if(estimatedTime == 3){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Preparing to install");
}
if(estimatedTime == 4){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Installing");
}
if(estimatedTime == 5){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Installed");
}
if(estimatedTime == 6){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Unzipping packages...");
}
estimatedTime = (System.nanoTime() - startTime) / 1000000000;
}
}