我关注 this tutorial在我的程序中有一个加载屏幕。该教程说我的 Activity 应该使用 Sleep() 命令进行 Sleep(),但是它不将 Sleep() 识别为一个函数,并给我一个错误,询问我是否要创建一个名为 Sleep() 的方法。
这是代码示例:
public class LoadingScreenActivity extends Activity {
//Introduce an delay
private final int WAIT_TIME = 2500;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("LoadingScreenActivity screen started");
setContentView(R.layout.loading_screen);
findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
//Simulating a long running task
this.Sleep(1000);
System.out.println("Going to Profile Data");
/* Create an Intent that will start the ProfileData-Activity. */
Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class);
LoadingScreenActivity.this.startActivity(mainIntent);
LoadingScreenActivity.this.finish();
}
}, WAIT_TIME);
}
}
最佳答案
您可以使用以下方法之一:
Thread.sleep(timeInMills);
或
SystemClock.sleep(timeInMills);
SystemClock.sleep(milliseconds)
是一个与 Thread.sleep(milliseconds)
非常相似的实用函数,但它忽略了 InterruptedException
。如果不使用Thread.interrupt()
,请使用此函数进行延迟,因为它将保留线程的中断状态。
关于java - Android Java 中的 sleep (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14941643/