为什么我看不到闪屏?

时间:2012-04-29 17:56:58

标签: android android-layout android-intent

我有下面的代码,它会创建一个启动画面。当我启动我的应用程序时,我不会查看启动画面,而是直接看到第一个活动:

public class splash extends Activity {
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 13000;
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);        
Intent intent = new Intent(this, prima.class);
splash = (ImageView) findViewById(R.id.imageView1);
Message msg = new Message();
msg.what = STOPSPLASH;       
splashHandler.sendMessageDelayed(msg, SPLASHTIME);        
startActivity(intent);  
}
}

1 个答案:

答案 0 :(得分:1)

您必须从处理程序执行startActivity()。如果你直接这样做,你将直接切换到下一个活动。

public class splash extends Activity {
    private static final int STOPSPLASH = 0;
    // time in milliseconds
    private static final long SPLASHTIME = 13000;
    private Intent nextActivity;

    // handler for splash screen
    private Handler splashHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case STOPSPLASH:
                    // start next Activity
                    startActivity(nextActivity);
                    break;
            }
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        nextActivity = new Intent(this, prima.class);
        splashHandler.sendEmptyMessageDelayed(STOPSPLASH, SPLASHTIME);
    }
}