2013-02-18 46 views
11

我正在學習本教程以在我的程序中加載屏幕。本教程指出我的活動應該使用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); 
    } 
} 
+6

Thread.sleep(1000); – 2013-02-18 17:12:05

+2

您正試圖將'sleep()'與Handler結合起來,這是不必要的,因爲'postDelay()'已經引入了延遲。如果你想延長時間,請增加'WAIT_TIME'。 – Sam 2013-02-18 17:14:34

+2

我覺得值得指出的是,爲了這樣做,使用加載屏幕並不是一個好主意。我明白你正在學習一個教程,這很好。但是一旦你開始爲用戶建立一些東西,請不要讓他們等待超過絕對必要的時間。如果你有數據要加載,那麼在加載時顯示一個閃屏,但不要硬編碼任意等待時間。你只是在浪費時間。 – FoamyGuy 2013-02-18 17:14:56

回答

31

可以使用folllowing方法之一:

Thread.sleep(timeInMills); 

SystemClock.sleep(timeInMills); 

SystemClock.sleep(milliseconds)是一個實用的功能非常相似,Thread.sleep(milliseconds),但是卻忽略了InterruptedException。如果您不使用Thread.interrupt(),請使用此功能延遲,因爲它會保留線程的中斷狀態。

4

您發佈的代碼非常糟糕。請不要在實際設備上使用它。如果您運行類似於此的操作,則會收到「應用程序未響應」錯誤。

如果您使用的是處理程序,請記住在其運行的線程上創建處理程序。因此,在UI線程上調用new Handler().post(...將在UI線程上執行可運行,包括此「長時間運行操作」。好處是您可以創建一個Handler到UI線程,稍後可以使用它,如下所示。

要將長時間運行的操作放到後臺線程中,您需要圍繞runnable創建一個Thread,如下所示。現在,如果要在長時間運行操作完成後更新UI,則需要使用處理程序將其發佈到UI線程。

請注意,該功能非常適合AsyncTask,這將使其看起來比以下圖案更清晰。但是,我將其包含在內以顯示Handlers,Threads和Runnables如何關聯。

public class LoadingScreenActivity extends Activity { 

//Introduce a delay 
    private final int WAIT_TIME = 2500; 
    private Handler uiHandler; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     uiHandler = new Handler(); // anything posted to this handler will run on the UI Thread 
     System.out.println("LoadingScreenActivity screen started"); 
     setContentView(R.layout.loading_screen); 
     findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE); 

     Runnable onUi = new Runnable() { 
      @Override 
      public void run() { 
       // this will run on the main UI thread 
       Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class); 
       LoadingScreenActivity.this.startActivity(mainIntent); 
       LoadingScreenActivity.this.finish(); 
      } 
     }; 

     Runnable background = new Runnable() { 
      @Override 
      public void run() { 
       // This is the delay 
       Thread.Sleep(WAIT_TIME); 
       // This will run on a background thread 
       //Simulating a long running task 
       Thread.Sleep(1000); 
       System.out.println("Going to Profile Data"); 
       uiHandler.post(onUi); 
      } 
     }; 

     new Thread(background).start(); 
} 
1

use Thread.sleep(1000);

1000是程序暫停的毫秒數。

try   
{ 
    Thread.sleep(1000); 
} 
catch(InterruptedException ex) 
{ 
    Thread.currentThread().interrupt(); 
} 

請記住:不建議使用此代碼,因爲它是延遲時間但沒有控制,可能需要更多或更少的時間。