29

I am following this tutorial to have a loading screen in my program. The tutorial says my activity should Sleep() using the Sleep() command, however it does not recognize Sleep() as a function and provides me with an error, asking if I would like to create a method called Sleep().

Here is the code sample:

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);
    }
}
jww
  • 97,681
  • 90
  • 411
  • 885
spogebob92
  • 1,474
  • 4
  • 23
  • 32
  • 6
    Thread.sleep(1000); – David M Feb 18 '13 at 17:12
  • 3
    You are trying to combine `sleep()` with Handler, this is unnecessary since `postDelay()` will already introduce a delay. If you want a longer delay, increase `WAIT_TIME`. – Sam Feb 18 '13 at 17:14
  • 2
    I feel like it is worth pointing out, that it is not a good idea to use a loading screen, just for the sake of doing so. I understand that you are following a tutorial, and that is fine. But once you get to the point of building something for users, please do not make them wait any amount of time more than is absolutely necessary. If you've got data to load, do that and show a splash while it is loading, but don't hardcode an arbitrary wait time. You'll just be wasting their time. – FoamyGuy Feb 18 '13 at 17:14
  • Thank you very much. However my next activity I am trying to load up has not loaded after the load screen. I am trying to load a web view. Don't s'pose you have any ideas? – spogebob92 Feb 18 '13 at 17:15
  • @FoamyGuy I am trying to make a loading screen so my webview has chance to load and doesnt leave the user with a white screen. Do you know any tutorials that would help with this? – spogebob92 Feb 18 '13 at 17:17
  • 1
    I don't have anything specific to point you to. But I can tell you that you won't be using a second activity. You'll have to put your splash as a hidden layout within your Activity that has the WebView, and you'll have to show/hide it instead of launching a different Activity. [Actually check out this site](http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/) he has a good overview of a solution that I think you can adapt to your situation. – FoamyGuy Feb 18 '13 at 17:21

4 Answers4

63

You can use one of the folllowing methods:

Thread.sleep(timeInMills);

or

SystemClock.sleep(timeInMills);

SystemClock.sleep(milliseconds) is a utility function very similar to Thread.sleep(milliseconds), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
8

The function is Thread.sleep(long).

Note, however, that you should not perform a sleep on the UI thread.

MByD
  • 135,866
  • 28
  • 264
  • 277
5

The code you posted is horrible. Please don't use that on an actual device. You will get an "Application Not Responding" error if you run something similar to this.

If you're using Handlers, keep in mind that a Handler is created on the thread where it runs. So calling new Handler().post(... on the UI thread will execute the runnable on the UI thread, including this "long running operation". The advantage is that you can create a Handler to the UI Thread which you can use later, as shown below.

To put the long running operation into a background thread, you need to create a Thread around the runnable, as shown below. Now if you want to update the UI once the long running operation is complete, you need to post that to the UI Thread, using a Handler.

Note that this functionality is a perfect fit for an AsyncTask which will make this look a lot cleaner than the pattern below. However, I included this to show how Handlers, Threads and Runnables relate.

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(); 
}
323go
  • 14,143
  • 6
  • 33
  • 41
3

use Thread.sleep(1000);

1000 is the number of milliseconds that the program will pause.

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

Keep in mind: Using this code is not recommended, because it is a delay of time but without control and may need more or less time.

Leon pvc
  • 31
  • 4