2012-06-13 70 views
0

我做了一個啓動畫面從教程,它在Android 2.3+,甚至在Android 4.0+上效果很好但是如果我在Galaxy Tab上運行它(Android 3.1)它是從TWICE開始申請。 真的很煩我能在3.1上解決這個問題嗎?Android Splashscreen或加載屏幕上的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兩次。

這是什麼?

Ëd I T:

我才意識到,如果我啓動景觀模式的應用程序,而這是濺/加載屏幕上的這個錯誤只發生。也許這是一個有價值的信息來解決這個問題..

+0

[!閃屏惡,不使用它們(http://android.cyrilmottier.com/?p=632) –

+0

:|這是一篇不錯的文章,但我必須使用它們。工作職責。 –

回答

2

我不認爲你應該使用睡眠。也許你可以試試這個。

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) { 
    } 
} 
+0

它的工作原理與我的解決方案一樣,但不能解決兩次Activity的啓動問題。但我會用這個..它只是幾行,很好的片段。 –

+0

我自己也有這個問題。我用我的代碼更新了我的代碼 – Slickelito

+0

沒有解決,但我只是意識到這個bug也發生在android 2.3手機上,如果我開始橫向模式。在肖像一切都很好。 –

0
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; 
    } 
}