문제

I have a SplashScreen activity at my android app that lasts for 3 seconds, and then it switches to MainActivity.

MainActivity plays a sound, and it has a button. My problem is that the sound plays when the SplashScreen activity is visible.

onResume at MainActivity calls for run(), run() sleeps for 2 seconds and plays the sound.

public void run() {

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    m.start();

    try {
        Thread.sleep((int) randomValue * 1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    m.seekTo(0);

    m.start();
    cpu.setText(EranThatWillShortYourDouble(randomValue));

    btn.setClickable(true);

}

@Override
protected void onResume() {
    super.onResume();
    run();
}

Instead of SplashScreen displaying for 3 seconds and switches to MainAcivity, It lasts 5 seconds and plays the sound when SplashScreen is visible.

What do I do?

EDIT: This is my SplashScreen activity:

public class SplashScreen extends Activity {

private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}

도움이 되었습니까?

해결책

Please take a look at AsyncTasks, Android Runnables and the ability to use a Handler to postDelayed.

Don't do thread.sleep in the main thread, it's really bad. :)

다른 팁

Do not use splash screens in Android. They are against the design guidelines and provide a poor UX.

http://developer.android.com/design/patterns/help.html#your-app

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top