1。。如下,我的代码已更新。
2。。每当我尝试使用进度计数对话框从服务器下载数据时,但下载开始时它就会显示计数百分比。
3。,但问题是,当百分比达到30%时,该对话框最多显示30%(对话框将被关闭(这意味着所有数据已成功下载))
4。但是我需要显示对话框,直到完成100%为止,然后应将其关闭。无论服务器中存在什么文件,大约5到6个文件即可。
@Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(BaseApplication.getInstance().getCurrentActivity());
this.progressDialog.setMessage("Downloading Please wait.");
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setMax(100);
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}
@Override
protected Boolean doInBackground(String... strings) {
// Here is my downloading code but i removed
DownloadData("" , "" , ""); //stuff
}
return true;
}
@Override
protected void onProgressUpdate(String... values) {
this.progressDialog.incrementProgressBy(5);
}
protected void onPostExecute(Boolean result) {
this.progressDialog.dismiss();
}
private void downloadData(String Folder, String Name, String URL) {
Log.i(TAG, "Coming to this downloadBookDetails ");
// int url1 = Integer.parseInt(pDownloadURL);
int len;
try {
// int urlsixe = pDownloadURL.length();
// Log.i(TAG, "URL lenght" + urlsixe);
URL url = new URL(URL);
Log.i(TAG, "pDownload URL" + url);
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
ucon.connect();
int lenghtOfFile = ucon.getContentLength();
Log.i(TAG, "lenght of file" + lenghtOfFile);
InputStream is = ucon.getInputStream();
Log.i(TAG, " InputStream" + is);
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
Log.i(TAG, " BufferedInputStream" + inStream);
File directory = new File(pMainFolder, pFileName);
Log.i(TAG, "File Name dir" + directory);
FileOutputStream outStream = new FileOutputStream(directory);
byte[] buff = new byte[5 * 1024];
long total = 0;
while ((len = inStream.read(buff)) != -1) {
total += len;
// publishProgress("" + (int) ((total * 100) / urlsixe));
// Log.i(TAG, "Progress: Bar with Count " + (int) ((total * 100) / lenghtOfFile));
outStream.write(buff, 0, len);
}
publishProgress();
outStream.flush();
outStream.close();
inStream.close();
} catch (Exception e) {
//Add Network Error.
Log.i(TAG, "Download Error Exception " + e.getMessage());
e.printStackTrace();
}
}
答案 0 :(得分:0)
在您的Asyntask onPostExecute中检查是否达到最大值。如果达到最大值,请关闭对话框
protected void onPostExecute(Boolean result) {
if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
this.progressDialog.dismiss();
}
}
答案 1 :(得分:0)
首先,使用进度栏中的setProgress(0)将进度设置为0
第二,您正在下载文件后更新进度栏。它可能无法正常运行。
在while循环中使用publishProgress()。您已经在下载方法开始时保存了文件长度。计算total +(len / lengthOfFile * 100)下载的文件百分比。现在,使用此信息发送进度条百分比的更新值,而不是硬编码增量ProgressBy(5)。
答案 2 :(得分:0)
您应将进度百分比通过import multiprocessing
import functools
def class_method_wrapper(obj, method_str, args):
return getattr(obj, method_str)(args)
class Bar(object):
def __init__(self):
self.pool = multiprocessing.Pool(3)
def foo(self, x):
return x * 2
def run(self, l):
# skip code ...
func = functools.partial(class_method_wrapper, self, 'foo')
foo_result = self.pool.map(func, l)
print foo_result
# skip code ...
def __getstate__(self):
self_dict = self.__dict__.copy()
del self_dict['pool']
return self_dict
def test1():
bar = Bar()
bar.run([1,2,3])
def test2():
bar = Bar()
pool = multiprocessing.Pool(2)
func = functools.partial(class_method_wrapper, bar, 'run')
args = []
for _ in range(10):
args.append([1, 2, 3])
pool.map(func, args)
if __name__ == '__main__':
test1()
#test2()
传递到onProgeressUpdate
尝试
publistProgress