我正在學習本教程以在我的程序中加載屏幕。本教程指出我的活動應該使用Sleep()命令進行Sleep(),但它不會將Sleep()識別爲函數,並向我提供錯誤,詢問是否要創建一個名爲Sleep()的方法。java中的Sleep()(Android)
這裏是到教程的鏈接:
http://androidcookbook.com/Recipe.seam;jsessionid=4DBCC1688B51DB16A2A40A86E135D361?recipeId=1599
下面是代碼示例:
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(1000); – 2013-02-18 17:12:05
您正試圖將'sleep()'與Handler結合起來,這是不必要的,因爲'postDelay()'已經引入了延遲。如果你想延長時間,請增加'WAIT_TIME'。 – Sam 2013-02-18 17:14:34
我覺得值得指出的是,爲了這樣做,使用加載屏幕並不是一個好主意。我明白你正在學習一個教程,這很好。但是一旦你開始爲用戶建立一些東西,請不要讓他們等待超過絕對必要的時間。如果你有數據要加載,那麼在加載時顯示一個閃屏,但不要硬編碼任意等待時間。你只是在浪費時間。 – FoamyGuy 2013-02-18 17:14:56