【问题标题】:How to make an activity flash at the beginning of the app?如何在应用程序开始时使活动闪烁?
【发布时间】:2015-06-05 00:22:20
【问题描述】:

我正在开发一个应用程序。在此,在应用程序开始时,一个活动将显示 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。需要帮助

【问题讨论】:

  • 基本上你已经用Thread.sleep(2000);阻塞了Ui线程......你应该找到更好的解决方案......提示:在“启动画面”中使用了类似的代码
  • 看看here。这几乎就是您想要做的。
  • Android 的 Google 启动画面

标签: java android


【解决方案1】:

您应该重新设计您的流程,以便:

open 是首先创建的 Activity,加载 MainActivity 后 2 秒。

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 活动开始

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多