【问题标题】:Android Splashscreen or loading screen on 3.13.1 上的 Android 启动画面或加载屏幕
【发布时间】:2012-06-13 07:07:45
【问题描述】:

我根据教程制作了一个启动画面,它在 Android 2.3+ 甚至在 Android 4.0+ 上运行良好,但如果我在我的 Galaxy Tab (Android 3.1) 上运行它,它是 启动 TWICE 应用程序。 真的很烦人,在 3.1 上我能做些什么来解决这个问题?

代码如下:

public class SplashScreen extends Activity 
{

protected boolean _active = true;
protected int _splashTime = 2000; // time to display the splash screen in ms

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

    try{

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    overridePendingTransition(0 , 0);

    // thread for displaying the SplashScreen
    Thread 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 {
               // finish();

                try{
                    Intent i = new Intent();
                    i.setClass(SplashScreen.this, MySecondActivity.class);
                    startActivity(i);
                    finish();
                }
                catch(Exception e)
                {
                    ki(e);
                }

            }
        }
    };

    splashTread.start();

    }catch(Exception ex)
    {
        ki(ex);
    }

 }

@Override
public void onBackPressed() {
   return;
}   



 //Toaster
    public void ki(Exception message)
    {
    Toast myToast = Toast.makeText(getApplicationContext(), message.toString(), 1);
    myToast.show();
}



}

如您所见,这是一个简单的线程活动更改,但是当它更改时,它会启动两次 MySecondActivity。

什么鬼?

编辑:

我刚刚意识到,这个错误只有在我在横向模式下启动应用程序时才会发生,而它是在启动/加载屏幕上。也许这是解决这个问题的宝贵信息..

【问题讨论】:

标签: android android-3.1-honeycomb


【解决方案1】:

我认为你不应该使用睡眠。也许你可以试试这个。

private void popSplash() {
    Message msg = splashHandler.obtainMessage();
    splashHandler.sendMessageDelayed(msg, 2000);
}

private Handler splashHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) 
    {
        super.handleMessage(msg);
        startActivity(new Intent(SplashScreen.this,MySecondActivity.class);;
        finish();
    }
};

只需在 onCreate 中调用 popSplash。


这对我有用 -

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Display d = getWindowManager().getDefaultDisplay();
            // For tablets, to avoid dublicate onCreates.
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    }
}

【讨论】:

  • 嗯,它就像我的解决方案一样工作,但不能解决两次 Activity 启动。但我会用这个..它只是几行,很好的sn-p。
  • 我自己也遇到过这个问题。我用适合我的 sn-p 更新了我的代码
  • 未解决,但我刚刚意识到,如果我以横向模式启动,Android 2.3 的手机也会出现此错误。在肖像中一切都很好。
【解决方案2】:
public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        // thread for displaying the SplashScreen
        Thread 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 {
                    finish();
                    Intent i = new Intent(SplashScreen.this, MyApp.class);
                    startActivity(i);
                }
            }
        };
        splashTread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多