Android的闪屏 [英] Android SplashScreen

查看:188
本文介绍了Android的闪屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发基本上下载大量数据的应用程序本身的开始和在ListActivity显示它的应用程序。我正打算做的是显示开机画面,直到数据加载。

I'm developing an application which basically downloads a lot of data at the start of the application itself and displays it in the ListActivity. What I'm planning to do is show a Splash Screen till the data is loaded.

到目前为止我所有的努力是徒劳的。我试过anddev.org提到的方法,但我的问题是,主要的活动应该开始,但闪屏应该是可见的,直到我填充我的ListActivity。因此,在短期我必须要经过以下步骤:

Till now all my attempts have been futile. I tried anddev.org mentioned methods, but my problem is that the main Activity should start but The Splash Screen should be visible till I populate my ListActivity. So in short I have to go through the following steps:

  1. 在开始我的主要活动。
  2. 显示开机画面。
  3. 在保持运行的进程在后台。
  4. 退出启动画面处理完成后,并显示在主列表。

希望你明白它是什么样......

Hope you understand what it is like....

推荐答案

现在的问题是最有可能的是正在运行的启动画面(某种的对话的如 ProgressDialog 我假设)在同一个线程中所做的所有工作。这将继续被更新的闪屏,它可以保持它甚至让显示在屏幕上的看法。你需要显示启动画面,打完折的AsyncTask 的去下载所有数据,然后隐藏启动画面,一旦任务完成。

The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.

所以,你的活动的onCreate()方法将简单地创建一个ProgressDialog并显示它。然后创建的AsyncTask并启动它。我会做的AsyncTask的一个内部类的主要活动,因此它可以存储已下载到一些变量在活动中的数据,并关闭ProgressDialog在其onPostExecute()方法。

So your Activity's onCreate() method would simply create a ProgressDialog and show it. Then create the AsyncTask and start it. I would make the AsyncTask an inner class of your main Activity, so it can store the data it has downloaded to some variable in your Activity and close the ProgressDialog in its onPostExecute() method.

不知道怎么了详细阐述,而不只是显示的code,所以这里是:

Not sure how to elaborate anymore without just showing the code, so here it is:

public class MyActivity extends Activity {
    private ProgressDialog pd = null;
    private Object data = null;

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

        // Show the ProgressDialog on this thread
        this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);

        // Start a new thread that will download all the data
        new DownloadTask().execute("Any parameters my download task needs here");
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {
         protected Object doInBackground(String... args) {
             Log.i("MyApp", "Background thread starting");

             // This is where you would do all the work of downloading your data

             return "replace this with your data object";
         }

         protected void onPostExecute(Object result) {
             // Pass the result data back to the main activity
             MyActivity.this.data = result;

             if (MyActivity.this.pd != null) {
                 MyActivity.this.pd.dismiss();
             }
         }
    }    
}

显然有您需要填写有一些作品,但code应该运行,并给你一个很好的起点(原谅我,如果有一个code错误,我没有访问在Android SDK因为我打字现在)。

Obviously there are some pieces you need to fill in there, but this code should run and give you a good starting point (forgive me if there is a code error, I don't have access to the Android SDK as I'm typing this currently).

一些更良好的阅读对Android的AsyncTasks的主题都可以在这里找到 这里

Some more good reading on the subject of AsyncTasks in Android can be found here and here.

这篇关于Android的闪屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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