user1486061
user1486061

Reputation: 35

Using the java timer

Until now I have been using the Timer that's in javax.swing.timer - all I had to do was to choose the speed and have a method with a specific name that executes what I want the timer to do every time.

I have now started programming an Android application recently and I have faced a couple of problems...

  1. It wouldn't let me use or import javax.swing.timer
  2. I tried using java.util.timer but I couldnt figure out how it works

All I want my timer for is so I can display my logo for 3 seconds and then proceed to the main menu - even if there is an easier solution for this, I'd still like to know how to use the timer

For the person who told me to try using a thread - here is my code - it doesnt paint the first screen at all, it just stays blank for 3 seconds then moves on to the next line of code...

public class logo extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            setContentView(R.layout.logoview);
            Thread.sleep(3000); 
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }

        setContentView(R.layout.main);
    }

    public void startup (View view){
        Intent intent0 = new Intent (this,ExpiramantingActivity.class);
        startActivity(intent0);}
    }
}

Upvotes: 1

Views: 550

Answers (4)

Mariusz Chw
Mariusz Chw

Reputation: 364

Maybe you can use a Thread? I think it's the simplest way to do what you want:

Where you display your logo:

try {
    //show your logo here
    Thread.sleep(3000); //3 seconds
} catch (InterruptedException e) {
    e.printStackTrace();
}

Surrounding the code in a try/catch block is very important, because of the posibility of an Exception.

Upvotes: 0

Mariusz Chw
Mariusz Chw

Reputation: 364

try this:

// logo
Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }; 
        };
        timer.start();

//next code

Upvotes: 0

sunil
sunil

Reputation: 6614

in onCreate() function of your SplashActivity class, schedule a handler to display your main menu activity after 3 seconds. this should be done after setContentView(R.layout.splashscreen);

    new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    startActivity(new Intent(SplashActivity.this,
                            YourMainMenuActivity.class));
                    finish();
                }
            }, 3000);

Upvotes: 1

rlinden
rlinden

Reputation: 2041

I believe that what you are trying to do is similar to the exhibition Splash Screen. If so, please check this Oracle tutorial.

Upvotes: 0

Related Questions