0

问题描述

我的应用程序中有两个活动MainActivityBannerActivity. 从主要活动开始,我从BannerActivity方法开始onCreate。但我首先看到MainActivity屏幕一秒钟然后BannerActivity屏幕。

问题

我如何做到这一BannerActivity点将首先显示,倒数计时器将停止并在屏幕出现BannerActivity后关闭。MainActivity

主要活动

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* Show banner activity for several seconds then close it. */
    Intent bannerIntent = new Intent(MainActivity.this, BannerActivity.class);
    this.startActivity(bannerIntent);   
}
};

横幅活动

public class BannerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* Make banner fullscreen. */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banner);

    /* Launch count down timer for several seconds. */
    CountDownTimer countDownTimer = new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) { /* Not used. */ }

        @Override
        public void onFinish() {
            BannerActivity.this.finish();
        }

    }.start();

}

@Override
public void onBackPressed() {
    /* Lock back button presses. */
    super.onBackPressed();
}

};
4

8 回答 8

1

你需要一个启动画面:

http://www.thiagorosa.com.br/en/tutorial/part01-splash-screen

public class GameSplash extends Activity {

// time to wait on the splash screen
private static final int SPLASH_SCREEN_DELAY = 3000;

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

    new Thread() {
        @Override
        public void run() {
            try {
                // do any heavy initialization here

                // wait a few seconds before going to the next screen
                sleep(SPLASH_SCREEN_DELAY);
            }
            catch (InterruptedException e) {

            }
            catch (Exception e) {

            }
            finally {
                // start the level selection screen
                Intent intentSelect = new Intent(GameSplash.this, GameSelect.class);
                startActivity(intentSelect);
            }
        }
    }.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // ignore any key press on the splash screen
    return true;
}

}
于 2013-05-27T12:49:50.957 回答
0

尝试使用这种方式首先需要加载xml布局

requestWindowFeature(Window.FEATURE_NO_TITLE);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banner);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

或尝试像

 <activity
        android:name=".BannerActivity"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           >
于 2013-05-27T12:23:29.357 回答
0

运行第一个 BannerActivity 并在 contdown 中运行 MainActivity 而不是 BannerActivity.this.finish();

于 2013-05-27T12:23:56.420 回答
0

您可以从 BannerActivity 开始,然后在时间到期时转到 MainActivity 并使用此意图标志清除历史堆栈(这样您就无法使用后退按钮返回到 BannerActivity)。

Intent.FLAG_ACTIVITY_CLEAR_TASK
于 2013-05-27T12:24:18.463 回答
0

你需要启动画面。试试这个:

首先开始BanerActivity。让它onCreate

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

        Thread welcomeThread = new Thread() {

            int wait = 0;

            @Override
            public void run() {
                try {
                    super.run();

                    while (wait < 5000) { //Wait of 5 seconds
                        sleep(500);
                        wait += 500;
                    }
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(BanerActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }

希望这可以帮助。

于 2013-05-27T12:26:09.677 回答
0

交换 Manifest 上的活动以决定首先运行哪个活动:

    <activity
        android:name="com.yourpackagename.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.yourpackagename.BannerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

BannerActivity 应该有<intent-filter>标签并设置为 Main 和 Launcher 作为示例。当您启动应用程序时,这将使 Banner 活动成为初始活动。

之后,您只需实现 countdowntimer 并在计时器结束时启动 MainActivity。

希望这会有所帮助,祝你好运^^

于 2013-05-27T12:28:41.367 回答
0

您也可以在横幅活动中这样做

在横幅活动的 oncreate 方法中使用此代码

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {

            finish();
        }
    }, 2000);
于 2013-05-27T12:30:27.993 回答
0

最简单的解决方案是制作BannerActivity启动器活动并将其修改CountDownTimer

 @Override
 public void onFinish() 
 {
         Intent mainIntent = new Intent(this, MainActivity.class);
         startActivity(mainIntent); 
         finish();
 }

然后将manifest文件 调整BannerActivity为 aslauncher activity并将其从MainActivity

于 2013-05-27T12:34:44.760 回答