【发布时间】:2015-01-24 09:41:00
【问题描述】:
我是安卓编程新手。我正在开发一个网络爬虫,我正在使用异步任务并且它运行良好。为了让用户了解情况,我正在使用进度对话框。我的问题是,如果我使用进度对话框,我的程序需要更多时间来执行,而当我不使用进度对话框时,它执行得更快。
完成工作 OnCreate 方法
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intent = getIntent();
s1 = intent.getStringExtra("Number1");
s2 = intent.getStringExtra("Number2");
s3=intent.getIntExtra("selectedItem",0);
HttpAsyncTask asyncTask = new HttpAsyncTask();
asyncTask.execute();
}catch (Exception e)
{
messageBox("Exception",e.getMessage());
}
}
异步任务类
private class HttpAsyncTask extends AsyncTask<List<String>, Integer, List<String>> {
private ProgressDialog dialog;
@Override
protected void onPreExecute()
{
dialog = new ProgressDialog(Results.this);
dialog.setIndeterminate(true);
dialog.setMessage("Please Wait");
dialog.setCancelable(true);
dialog.show();
super.onPreExecute();
}
@Override
protected List<String> doInBackground(List<String>... urls) {
//android.os.Debug.waitForDebugger();
// spinner.setVisibility(View.VISIBLE);
List<String>resultList=new ArrayList<String>();
try
{
if(isCancelled())
return resultList;
resultList=WebCrawlerClass.GetPost(s1,s2,s3);
}catch (Exception e)
{
messageBoxs("Error", e.getMessage());
}
return resultList;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(List<String> result)
{
if(dialog.isShowing())
{
dialog.dismiss();
}
if(s3 == 2)
{
docListAdapter=new ListViewData(Results.this,result);
}
else {
docListAdapter = new NameNumListData(Results.this, result);
}
docList=(ListView)findViewById(R.id.listView2);
docList.setAdapter(docListAdapter);
super.onPostExecute(result);
}
@Override
protected void onCancelled() {
super.onCancelled();
this.cancel(true);
}
}
我错过了什么吗?需要帮助..
感谢和问候, 阿比纳夫
【问题讨论】:
-
这可能是因为 onPreExecute() 中的工作。如果您在活动中启动对话框并使用 Handler 或任何回调方法通知您asynctask 已完成工作的活动,以便您可以停止进度对话框,进度对话框的额外负载将从 asynctask 中删除
-
@IchigoKurosaki 谢谢你……让我试试,我会去找你的……
-
@IchigoKurosaki 如果你能给我一些链接...那就太好了。
-
我会为你设计答案..
标签: java android android-asynctask progressdialog