显示启动画面,直到应用程序完成加载

时间:2013-03-04 16:17:48

标签: java android xml android-asynctask splash-screen

我的应用程序在启动时加载了很多东西,经过测试,它在开始时延迟太久而没有splash screen。所以,我希望在我的应用加载完成之前显示splash screen。我不希望在X秒内显示带有计时器的屏幕。我在这里找到了一个例子:

Android SplashScreen

我尝试在上面的SO主题中实现代码,但我不理解代码。在将它集成到我的代码中后,我想出了一个错误,我在下面的代码中注释了它。但我不了解很多代码,我在下面的代码中评论了我感到困惑的部分。

public class MainMenu extends Activity {

    private ProgressDialog pd = null;
    private Object data = null;  //What is this?

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

        // 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 to download.");  //What is DownloadTask()?
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {

        protected Object doInBackground(String... args) {  //Are these parameters correct?
            return "replace this with your object";  //What is this?
        }

        protected void onPostExecute(Object results) {
            // pass the resulting data to the main activity
            MainMenu.this.data = result;  //Error:  "result cannot be resolved to a variable"

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

2 个答案:

答案 0 :(得分:3)

让我们从错误开始:

MainMenu.this.data = result;

注意拼写错误?它应该是结果* s *:

MainMenu.this.data = results;

解决下面的其余问题:

private class DownloadTask extends AsyncTask<String, Void, Object>

声明适用于名为DownloadTask的内联类,它声明您将String(通过String...)作为doInBackground(String... params)的参数。

第二个参数(在您的情况下为Void)表示用于通过publishProgress(DATATYPE) / onProgressUpdate(DATATYPE... progress)“发布”进度的数据类型。此方法适用于通知用户更改,例如,当您下载完文件但仍有少量文件时。

最后一个参数(Object)表示您将传递给onPostExecute(DATATYPE)的数据类型,在此示例中为Object。这可以是在某处更新ListAdapter,也可以根据doInBackground中完成的操作的结果触发任何其他UI更改。

答案 1 :(得分:2)

ProgressDialog中显示onPreexecute并在onPostExcute方法中将其关闭

类似这样的事情

private class DownloadTask extends AsyncTask<String, Void, Object> {

           @Override
protected void onPreExecute() {
  mProgressDialog = new ProgressDialog(activity);
    mProgressDialog =ProgressDialog.show(activity, "", "Please Wait",true,false);
    super.onPreExecute();
}

        protected Object doInBackground(String... args) {  //Are these parameters correct?
            return "replace this with your object";  //What is this?
        }

        protected void onPostExecute(Object results) {
            // pass the resulting data to the main activity
            MainMenu.this.data = results;  //it should be results 
if (mProgressDialog != null || mProgressDialog.isShowing()){
         mProgressDialog.dismiss();
 }
            if(MainMenu.this.pd != null) {
                MainMenu.this.pd.dismiss();
            }
        }