【发布时间】:2015-08-19 13:01:23
【问题描述】:
我正在尝试为我的应用制作启动画面。我使用了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 课程已收费并显示日志,但我没有在屏幕上看到它!
我想知道有什么问题。
谢谢
【问题讨论】:
-
在您的
run方法中添加一些Log.d调用 -
问题是你正在调用 mEndSplash.run();来自 onTouchEvent(MotionEvent 事件)。所以它只有在你点击屏幕时才有效。
-
@techroid 我也把它称为 postdelayed
-
显示你在哪里使用这条线
mHandler.postDelayed(mEndSplash, SPLASH_DURATION_MS); -
SPLASH_DUR_MS 的值是多少?顺便说一句,这不是飞溅的持续时间,而是启动它的延迟。
标签: android android-runonuithread