启动画面问题

时间:2011-05-09 07:03:48

标签: android splash-screen

我正在使用以下代码创建启动画面,当我按回键时,应用程序移动到主屏幕,几秒钟后显示我的下一个主菜单屏幕。我在onBackPressed()中调用finish(),我想要在启动画面中按回键关闭应用程序。任何人都可以帮我解决这个问题吗?

谢谢!

     Thread splashThread = new Thread() {
        @Override
        public void run() {
           try {
              int waited = 0;
              while (_active && (waited < 2000)) {
                 sleep(100);
                 if(_active) {
                     waited += 100;
                 }
              }
           } catch (InterruptedException e) {
              // do nothing
           } finally {

               finish();
               startActivity(new Intent("next activity"));
               stop();
           }
        }
     };
     splashThread.start();

4 个答案:

答案 0 :(得分:2)

这是因为您在finish();

之前致电startActivity(new Intent("next activity"));

使用finish();

交换startActivity(new Intent("next activity"));

答案 1 :(得分:0)

尝试使用:

SplashScreen.this.finish();

其中SplashScreen是Activity的名称

答案 2 :(得分:0)

它正在我的应用程序中工作

public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }

            } catch(InterruptedException e) {
                // do nothing
            } finally {

                if(!stop){
                    startActivity(new Intent(Splash.this,Home.class));
                    finish();
                }
                else
                    finish();
            }
        }

    };
    splashTread.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        if(splashTread.isAlive())
            this.stop = true;
    }
    return true;
}

}

答案 3 :(得分:0)

此解决方案仅解决了后退按钮的问题。如果用户按下主页按钮,仍会发生不需要的行为。覆盖onStop方法并在那里做你的事情会不会更容易?

@Override
public void onStop(){
    super.onStop();
    if(splashTread.isAlive())
       this.stop = true;
}
相关问题