2015-03-31 37 views
-1

我正在開發一個應用程序。在這個應用程序開始時,一個活動將顯示2秒,之後該應用程序以主要活動開始。 這是我試圖做到這一點:如何在應用程序開始時使活動閃爍?

的activity_main.java文件:

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Intent i = new Intent(MainActivity.this,open.class); 
    startActivity(i); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    android.app.ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F"))); 
    bar.setSubtitle(R.string.title_sub); 
} 

}

的open.java文件

public class open extends Activity { 
protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.open); 
     android.app.ActionBar bar = getActionBar(); 
     bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F"))); 
} 
protected void onStart(){ 
    super.onStart(); 
    try { 
      Thread.sleep(2000); 

     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     finish(); 
} 

}

的open.java文件應該在應用程序的開始處顯示2秒鐘恩將繼續執行,從activity_main.java

但發生的事情是一個空白屏幕顯示了一秒鐘,然後在MainActivity是shown.Need幫助

+0

基本上已阻塞UI線程與'的Thread.sleep(2000);'...你應該找到更好的解決方案......提示:在「啓動畫面」裏使用類似的代碼 – Selvin 2015-03-31 15:32:23

+1

看看[這裏](http://stackoverflow.com/a/4114569/2835243)。這幾乎是你想要做的。 – TheWanderingMind 2015-03-31 15:34:44

+0

Android的Google splash屏幕 – Apurva 2015-03-31 15:39:59

回答

0

你應該重新設計你的流量,使:

open是加載MainActivity之後首先創建的Activity,以及2秒後創建的Activity。

public class open extends Activity { 
    private Activity mActivity; 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.open); 
    android.app.ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F"))); 
    mActivity = this; 
    Handler handler = new Handler(); 
    Runnable r = new Runnable() { 
     public void run() { 
     Intent i = new Intent(mActiviy, MainActivity.class); 
     startActivity(i); 
     } 
    } 
    handler.postDelayed(r, 2000); 
    } 
} 

最後一件事就是確保你換你的清單中的活動,使您的應用程序開始與open活動

相關問題