关于用于创建加载屏幕的AsyncTask

时间:2012-03-20 20:59:21

标签: android

我在如何使用progressDialog在AsyncTask上实现“创建加载屏幕”方面处于两难境地。

我正在做的是我有一个不需要输入的方法,只显示一个textview。

  

私有类DownloadFilesTask扩展了AsyncTask {

所以我的方法所做的就像1 + 1 = 2,但是以更复杂的方式存储一个giganormous字符串,然后在textview上显示。

所以我的问题是如何在长时间加载之前实现进度对话框的结果?

2 个答案:

答案 0 :(得分:4)

Android内置了progress dialog class。我推荐使用它,除非它有一些不适合你的需求。

答案 1 :(得分:2)

private progressDialog pd;
public void runMethod(){
new doStuuff.execute();
}


private Class doStuff extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
//Everything in your code except for anything that is xml or relates with any view. Mostly calculations
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
//Your main background view code ends up falling here
            pd.dismiss();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
//Your optional view and where it starts
pd = ProgressDialog.show(View_Book_As_Whole.this, "Loading", "Please wait...");
        }

}

我太不知道这个概念但是一旦理解了它就相对容易了。 onPreExecute()意味着它在任何东西之前运行,它首先获得优先级然后转到doInBackground(),然后onPostExecute()它运行最终任务。这一切都在UI的背景下工作。