我当前的项目涉及从服务器下载数据 - 这需要大约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();
}
}
答案 0 :(得分:0)
示例代码看看是否有效?如果这有效,则表示您在视图中显示和隐藏
时出现了一些错误<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/download"
android:textSize="14sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/your_image_will_appear_here" />
</LinearLayout>
你的java类
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** Reference to the view which should receive the image */
private final WeakReference imageRef;public DownloadImageTask(ImageView imageView) {
imageRef = new WeakReference(imageView);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(DownloadImageActivity.this, "Wait", "Downloading...");
}
@Override
protected Bitmap doInBackground(String... params) {
InputStream input = null;
try {
URL url = new URL(params[0]);
// We open the connection
URLConnection conection = url.openConnection();
conection.connect();
input = new BufferedInputStream(url.openStream(), 8192);
// we convert the inputStream into bitmap
bitmap = BitmapFactory.decodeStream(input);
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return bitmap;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(Bitmap bitmap) {
progressDialog.dismiss();
if (isCancelled()) {
bitmap = null;
}
if (imageRef != null) {
ImageView imageView = imageRef.get();
if (imageView != null && bitmap != null) {
imageView.setImageBitmap(bitmap);
} else
Toast.makeText(DownloadImageActivity.this, "Error while downloading the image!", Toast.LENGTH_LONG).show();
}
}
}
<uses-permission android:name="android.permission.INTERNET"/>
答案 1 :(得分:0)
感谢您的回复,但我已经找到了问题。
我没有发布我的所有代码 - 我实际上在AsyncTask之后调用了另一个函数,一旦我将new DownloadWebpageTask.execute(String url)
之后的所有代码移动到onPostExecute()
,我的进度条就显示了!