我正在关注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/