2013-02-18 27 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(); 
} 

请记住:不建议使用此代码,因为它是延迟时间但没有控制,可能需要更多或更少的时间。