android postDelayed方法不起作用

时间:2015-08-19 13:01:23

标签: android android-runonuithread

我正在尝试为我的应用设置启动画面。我使用了postDelayed方法。这是我的代码:

   public class SplashScreenActivity extends Activity {

    private static final int SPLASH_DURATION_MS = 1500;

    private static final String TAG = SplashScreenActivity.class.getSimpleName();

    private Handler mHandler = new Handler();

    public static final int sdkVersion = Build.VERSION.SDK_INT;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        Random r = new Random();
        int imageNumber = r.nextInt(2 - 0) + 0;

        ImageView splashScreenBackground = (ImageView)findViewById(R.id.splash_screen_image);
        switch (imageNumber){
            case 0:
                if(sdkVersion > 20)
                    splashScreenBackground.setBackground(getDrawable(R.drawable.splash_screen_back));
                else
                    splashScreenBackground.setBackgroundDrawable(getResources().getDrawable(R.drawable.splash_screen_back));
                return;
            case 1:
                if(sdkVersion > 20)
                    splashScreenBackground.setBackground(getDrawable(R.drawable.buffon));
                else
                    splashScreenBackground.setBackgroundDrawable(getResources().getDrawable(R.drawable.buffon));
                return;
        }

        mHandler.postDelayed(mEndSplash, SPLASH_DURATION_MS);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mEndSplash.run();
        return super.onTouchEvent(event);
    }

    private Runnable mEndSplash = new Runnable() {
        public void run() {
            if (!isFinishing()) {
                mHandler.removeCallbacks(this);

                Log.d(TAG, "Debugging before intent");
                startActivity(new Intent(
                        SplashScreenActivity.this, MainActivity.class
                ));
                Log.d(TAG, "Debugging after intent");
                finish();
            }
        };
    };

}

这是onCreate方法中的调用:

mHandler.postDelayed(mEndSplash, SPLASH_DURATION_MS);

问题是,直到我按下屏幕时,活动才会显示。

另一件事,班级MainActivity被收取费用并显示日志,但我没有在屏幕上看到它! 我想知道问题是什么。 感谢

3 个答案:

答案 0 :(得分:2)

您在交换机中使用return而不是break,因此永远不会到达带有处理程序的代码。

答案 1 :(得分:0)

ImageView splashScreenBackground = (ImageView)findViewById(R.id.splash_screen_image);

splashScreenBackground.setImageResource(R.drawable.splash_screen_back);

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

        @Override
        public void run() {
    startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));

    }
  }, 1500);

答案 2 :(得分:-1)

在你的Splash Activity中尝试以下代码。

首先,声明以下

private Timer mTimer;// timer for running splash screen for 1500 millisecods(i.e. 1.5 seconds)

然后,在onCreate()中,输入以下代码

mTimer = new Timer();
    mTimer.schedule(new TimerTask() {
        @Override
        public void run() { 

            startActivity(new Intent(
                    SplashScreenActivity.this, MainActivity.class
             mTimer.cancel();
            SplashScreenActivity.this.finish();

        }
    }, 1500);