【发布时间】:2014-10-23 05:49:59
【问题描述】:
我目前的项目涉及从服务器下载数据 - 这大约需要 20 秒,所以在此期间我想显示一个进度条,以便用户知道发生了什么。下载任务在 DownloadWebpageTask (AsyncTask) 中执行,我在 preExecute() 中设置了进度条可见性,但它没有显示出来。我已经在这里查看了一大堆问题,但我仍然无法让它发挥作用。如果我不隐藏 onPostExecute() 中的进度条,我可以看到发生了什么——即使我在 onPreExecute() 中设置了 progBar.setVisibility(0),该条实际上直到 doInBackground 之后才显示。我也尝试在 onPreExecute 中设置一个 TextView 以查看它是否只是进度条不起作用,但这也失败了。
请有人帮帮我,我完全被卡住了!!
这调用了我的 AsyncTask:
public void getDTCs(View view) throws URISyntaxException{
DtcListSingleton.getInstance().setData(null);
status = "DTC Parsing Not Attempted";
noDTCs = 0;
TextView listTitle = (TextView) findViewById(R.id.DTCtitle);
String newTitle = "Waiting for DTCs";
listTitle.setText(newTitle);
String stringURL = "http://192.168.1.129";
//Check connection
ConnectivityManager cMgr = (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo netInf = cMgr.getActiveNetworkInfo();
// If connected to network carry on with DTC request
if(netInf != null && netInf.isConnected()){
//Download the web page as an InputStream
new DownloadWebpageTask().execute(stringURL);
这是我的 AsyncTask:
private class DownloadWebpageTask extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
progBar = (ProgressBar)findViewById(R.id.loadingDTCs);
progBar.setVisibility(0);
TextView title = (TextView)findViewById(R.id.DTCtitle);
title.setText("Loading DTCs");
}
@Override
protected String doInBackground(String...urls){
//params[0] is the url
try{
return downloadUrl(urls[0]);
}
catch(IOException e){
return "Unable to retreieve web page. URL may be invalid.";
}
// DELETE if overcome client.print delay
catch (InterruptedException e) {
e.printStackTrace();
return "Interrupted Exception from Delaying inputstream";
}
}
@Override
protected void onPostExecute(String result){
AlertDialog.Builder resultURL = new AlertDialog.Builder(ActionReadDTCs.this);
resultURL.setTitle("Result of DownloadWebPageTask")
.setMessage(result)
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
progBar = (ProgressBar)findViewById(R.id.loadingDTCs);
progBar.setVisibility(8);
return;
}
})
.show();
}
}
【问题讨论】:
-
你用 .setVisibility(View.VISIBLE) 试过了吗
-
您的最小/最大 SDK 和当前版本的 android 版本?
-
它会改变文本吗? title.setText("加载 DTC");??
-
是的,我用 View.VISIBLE 试过了,还是同样的问题。它也没有显示文字。
-
其实我想知道...在我的 AsyncTask 中,我正在调用其他几个任务,例如 downloadUlr 等 - 这些都需要在 AsyncTask 中声明吗?
标签: android android-asynctask progress-bar