当前位置: 首页 > 面试题库 >

Android SplashScreen

林泰平
2023-03-14
问题内容

我正在开发一个应用程序,它基本上会在应用程序本身的开始处下载大量数据,并将其显示在ListActivity中。我打算做的是显示一个启动屏幕,直到加载数据。

到现在为止,我所有的尝试都是徒劳的。我尝试了anddev.org提到的方法,但是我的问题是主要的Activity应该开始,但是在我填充ListActivity之前,初始屏幕应该是可见的。简而言之,我必须执行以下步骤:

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

希望你了解它的样子。


问题答案:

问题很可能是您在完成所有工作的同一线程中运行启动屏幕(某种对话框,例如我假设的ProgressDialog)。这将使初始屏幕的视图不会被更新,甚至可以使其不显示在屏幕上。您需要显示初始屏幕,启动AsyncTask实例以下载所有数据,然后在任务完成后隐藏初始屏幕。

因此,您Activity的onCreate()方法将只创建一个ProgressDialog并显示它。然后创建AsyncTask并启动它。我将AsyncTask设置为主活动的内部类,以便它可以将已下载的数据存储到活动中的某个变量中,并在其onPostExecute()方法中关闭ProgressDialog。

不知道如何仅在不显示代码的情况下进行详细说明,所以这里是:

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();
             }
         }
    }    
}

显然,您需要填写一些内容,但是此代码应该可以运行并为您提供一个良好的起点(请原谅我,如果有代码错误,我在输入此代码时将无法使用Android SDK)目前)。



 类似资料:

相关阅读

相关文章

相关问答