1

我的 android 应用程序有一个 SplashScreen 活动,持续 3 秒,然后切换到 MainActivity。

MainActivity 播放声音,它有一个按钮。我的问题是当 SplashScreen 活动可见时会播放声音。

MainActivity 的 onResume 调用 run(),run() 休眠 2 秒并播放声音。

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();
}

不是 SplashScreen 显示 3 秒并切换到 MainAcivity,而是持续 5 秒并在 SplashScreen 可见时播放声音。

我该怎么办?

编辑:这是我的 SplashScreen 活动:

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);
}

}

4

2 回答 2

1

不要在 Android 中使用闪屏。它们违反了设计准则并提供了糟糕的用户体验。

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

于 2014-05-09T17:23:38.113 回答
1

请查看AsyncTask s、Android Runnables以及使用Handler来 postDelayed 的能力。

不要在主线程中做thread.sleep,真的很糟糕。:)

于 2014-05-09T17:24:27.807 回答