繁体   English   中英

如何在Android应用程序中实现线程概念?

[英]How to implement thread concept in Android application?

我已经实现了一个基本的android applicaiton。我想使用线程概念。

当应用程序将打开时,一个启动屏幕将显示一段时间(3秒),然后打开新视图。

我知道如何打开下一个视图,但我不知道如何显示启动屏幕3秒钟。

恳求使我对此有所了解。

提前致谢。

CountDownTimer将是个好主意!

  public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.mylayout);

    lTimer = new CountDownTimer(3000, 1000) {

        public void onFinish() {
        closeScreen();
        }
        @Override
        public void onTick(long millisUntilFinished) {
        }
    }.start();
 }

 private void closeScreen() {
        Intent lIntent = new Intent();
        lIntent.setClass(getApplicationContext(), MainActivity.class);
            startActivity(lIntent);
            finish();
        }
      }

我不会提供有关此问题的任何代码,因为让您了解启动屏幕的用途更为重要。

当您的应用程序需要执行某种类型的初始化过程时(例如,您的应用程序依赖于它必须获取并解析的网络数据),会使用启动屏幕。 它可以在用户等待时向用户展示一些东西。 在任何情况下,都不应在没有充分理由的情况下使用启动屏幕,这意味着仅显示启动屏幕一段时间,而无需在后台执行任何重要操作。

我认为您正在寻找:

Splash_Activity的代码

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

    // thread for displaying the SplashScreen
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;


       while(_active && (waited < 3000)) {
                sleep(100);
                if(_active) {
                    waited += 100;
                }
            }
        } catch(InterruptedException e) {
            // do nothing
        } finally {
            finish();
            Intent intent = new Intent(getBaseContext(), nextActivity.class);
            startActivity(intent);
            stop();
        }
    }
};
splashTread.start();
}

这也是一个很好的例子http : //www.androidpeople.com/android-loading-welcome-splash-spash-screen-example/

new Handler().postDelayed(new Runnable() 
        {
            @Override
            public void run() 
            {

                Intent in = new Intent(SplashActivity.this,NextActivity.class);
                startActivity(in);
                    finish();
            }
        },3000);

您可以像在普通的老式Java应用程序中使用它们一样使用线程:)。 尝试使用google ,那里有一些很好的例子。 这是我刚发现的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM