2015-03-31 132 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活动

相关问题