如何在5秒后自动显示活动?

发布于 2024-11-15 02:15:27 字数 128 浏览 4 评论 0原文

在我的应用程序中,我在 Android 中创建了一个闪屏类型的东西。它应该保持 5 秒钟。

我的问题是如何在 5 秒后自动显示另一个活动?

启动屏幕没有按钮,而是应该在 5 秒后自动显示另一个活动,而无需单击按钮。

In my application I have created a splash screen type of thing in Android. It should remain for 5 seconds.

My problem is how do I display another activity automatically after 5 secs?

The splash screen doesn't have a button, rather it should display another activity automatically after 5 seconds without the click of a button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东京女 2024-11-22 02:15:27
new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final Intent mainIntent = new Intent(LaunchActivity.this, HomeActivity.class);
                LaunchActivity.this.startActivity(mainIntent);
                LaunchActivity.this.finish();
            }
        }, 5000);
new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final Intent mainIntent = new Intent(LaunchActivity.this, HomeActivity.class);
                LaunchActivity.this.startActivity(mainIntent);
                LaunchActivity.this.finish();
            }
        }, 5000);
柏林苍穹下 2024-11-22 02:15:27
TimerTask task = new TimerTask() {

            @Override
            public void run() {
                Intent intent = new Intent(SplashScreen.this, MainMenu.class);
                startActivity(intent);
                finishscreen();
            }
        };
        Timer t = new Timer();
        t.schedule(task, 5000);

private void finishscreen() {
        this.finish();
    }
TimerTask task = new TimerTask() {

            @Override
            public void run() {
                Intent intent = new Intent(SplashScreen.this, MainMenu.class);
                startActivity(intent);
                finishscreen();
            }
        };
        Timer t = new Timer();
        t.schedule(task, 5000);

and

private void finishscreen() {
        this.finish();
    }
心房敞 2024-11-22 02:15:27

您可以在此处使用线程
例如

// thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(500);
                        if(_active) {
                            waited += 500;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // start your activity here using startActivity
                    stop();
                }
            }
        };
        splashTread.start();

You can use thread here
For example

// thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(500);
                        if(_active) {
                            waited += 500;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // start your activity here using startActivity
                    stop();
                }
            }
        };
        splashTread.start();
尤怨 2024-11-22 02:15:27

这也可以使用 android CountDownTimer 类来完成。

请参阅此示例了解 5 秒 延迟。

new CountDownTimer(5000, 1000) {
    public void onFinish() {
         Intent startActivity = new Intent(ThisActivity.this,ActivityToStart.class);
         startActivity(startActivity);
        finish();
    }

    public void onTick(long millisUntilFinished) {
    }

}.start();

您可能还需要在 AndroidManifest.xml 文件中定义父 Activity,

<activity
      android:name=".ActivityToStart"
      android:label="Back"
      android:parentActivityName=".MainActivity" >

      <!-- Parent activity meta-data to support 4.0 and lower -->
      <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value=".MainActivity" />
</activity>

This can also be done using android CountDownTimer class.

See this example for 5seconds delay.

new CountDownTimer(5000, 1000) {
    public void onFinish() {
         Intent startActivity = new Intent(ThisActivity.this,ActivityToStart.class);
         startActivity(startActivity);
        finish();
    }

    public void onTick(long millisUntilFinished) {
    }

}.start();

You may also need to define your parent activity in AndroidManifest.xml file,

<activity
      android:name=".ActivityToStart"
      android:label="Back"
      android:parentActivityName=".MainActivity" >

      <!-- Parent activity meta-data to support 4.0 and lower -->
      <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value=".MainActivity" />
</activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文