Android加载时显示Splash-Screen [英] Android display Splash-Screen while loading

查看:206
本文介绍了Android加载时显示Splash-Screen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,显示启动画面3秒钟。之后,MainActivity被加载。

I have an Android App, which shows a "Splash Screen" for 3 seconds. After that, the MainActivity gets loaded.

不幸的是,MainActivity需要额外的~4秒才能加载。在第一次启动时甚至更长。但是当加载App时,一切都运行顺畅。

Unfortunately the MainActivity takes additional ~4 seconds to load. On first startup even longer. However when the App is loaded, everything runs smooth.

现在,在Splash Screen的显示过程中,如何实现MainActivity的加载?它应该显示一个图像,直到整个东西完全加载。
我已经阅读了有关Async-Task的内容,但我不知道该放在哪里以及如何正确使用它。有人可以帮帮我吗?

Now how can I achieve it, that the MainActivity gets loaded, during the display of the Splash Screen? It just should display an image until the whole thing loaded completely. I have read about Async-Task, but I'm not sure where to put it and how to use it properly. Can someone help me please?

SplashScreen.java

public class SplashScreen extends Activity {
    private static int SPLASH_TIME_OUT = 3000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
}

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {

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

        //Some heavy processing
        //starting services
        //starting Google Text to Speech
        //and so on...

    }

}


推荐答案

如果没有关于启动屏幕显示时间的具体限制,您可以通过以下方式使用 AsyncTask

If there are no specific constraints about the time the splash screen should be shown, you could use the AsyncTask in the following way:

public class SplashScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        startHeavyProcessing();

    }

    private void startHeavyProcessing(){
       new LongOperation().execute("");
    }

    private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            //some heavy processing resulting in a Data String
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            return "whatever result you have";
        }

        @Override
        protected void onPostExecute(String result) {
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            i.putExtra("data", result);
            startActivity(i);
            finish();
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
}

如果结果数据的性质不同于你可以将一个 Parcelable 对象作为一个字符串作为你的活动的额外内容。在 onCreate 中,您可以使用以下方法检索数据:

If the resulting data if of another nature than a String you could put a Parcelable Object as an extra to your activity. In onCreate you can retrieve the data with:

getIntent()。getExtras.getString ('数据');

这篇关于Android加载时显示Splash-Screen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆