- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试更新进度对话框以增加 1-100% 之类的值。但是,没有任何东西会增加。它总是只显示 0。尽管如此,我能够在我的 Log Cat 中看到打印出的数字,并且一切都在 100 之后结束。
有人看到我错过了什么吗?在此先感谢您的帮助。
private class DownLoadSigTask extends AsyncTask<String, Integer, String> {
private final ProgressDialog dialog = new ProgressDialog(NasStorageNasList.this);
// Set the fileName and filesize to be used later
String fileName = (String) recordItem.get(mPresentDown).get("name");
String filesize = (String) recordItem.get(mPresentDown).get("filesize");
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setMessage("Downloading " + fileName + " from the server. Please wait.");
this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.dialog.setCancelable(false);
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
@Override
protected String doInBackground(final String... args) {
// Here is where we need to do the downloading of the
try {
File destDir = new File(LOCALDOWNROOT);
if (!destDir.exists()) {
destDir.mkdirs();
}
File outputFile = new File(destDir, fileName);
Log.e("What is the filename path to download? ", REMOTEDOWNROOT + DATADIRECTORY + fileName);
InputStream fis = sardine.get(REMOTEDOWNROOT + DATADIRECTORY + "/"
+ fileName.replace(" ", "%20"));
FileOutputStream fos = new FileOutputStream(outputFile);
byte[] buffer = new byte[1444];
Log.e("What is the length of the File Size? ", filesize);
long total = 0;
int byteread = 0;
while ((byteread = fis.read(buffer)) != -1) {
downloadTotal += byteread;
// Here is the WHILE LOOP that we will want to give the user
publishProgress((int)(total*100/Long.parseLong(filesize)));
Log.e("CurrentAmount Downloaded: ", String.valueOf((int)(downloadTotal*100/Long.parseLong(filesize))));
// How can I tell the UI the downloaded percentage?
// So, far I am updating the log cat...but nothing for the user.
fos.write(buffer, 0, byteread);
}
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
// Just Return null because the void task
return null;
}
// add in a progress bar update
@Override
protected void onProgressUpdate(Integer...progress) {
this.dialog.setProgress(progress[0]);
}
// can use UI thread here
@Override
protected void onPostExecute(final String errMsg) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Toast.makeText(NasStorageNasList.this, "File Download Done!",
Toast.LENGTH_SHORT).show();
}
}// end DownloadSigTask
最佳答案
您有“打字错误”错误。您需要将 publishProgress()
方法中的 total 变量替换为 downloadTotal。
publishProgress((int) (downloadTotal * 100 / Long.parseLong(filesize)));
您的total 变量在循环之前仅被分配为零,并且永远不会更改为其他值。因此,您执行的除法 0/filesize
始终为 0。
关于Android AsyncTask publishProgress 不更新对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15793197/
当我尝试下载文件时,应用程序崩溃。 问题是当我刷新进度条时。我使用asynctask.The错误是这样的: while ((read = is.read(buffer, 0, 1024)) > 0)
我写了一个AsyncTask: public class AudioTransition extends AsyncTask { private int goalID; priv
这就是我遇到的问题:每当 publishProgress(sentCount);在应用程序崩溃时被调用。我环顾四周,仍然不知道这是什么原因。任何帮助是极大的赞赏!谢谢 class MySmsTask
我正在尝试更新进度对话框以增加 1-100% 之类的值。但是,没有任何东西会增加。它总是只显示 0。尽管如此,我能够在我的 Log Cat 中看到打印出的数字,并且一切都在 100 之后结束。 有人看
我有一个 AsyncTask,它使用轮询队列来查看是否有新对象到达。当它检测到新对象时,我让它以字符串形式收集信息,然后发布进度(信息)。在 onProgressUpdate 中,它将字符串添加到列表
我正在尝试在进度对话框中显示 3 条不同的消息。 在 onPreExecute 中,我创建对话框并设置第一条消息。 在 doInBackground 中,我调用 publishProgres("sen
假设我有一个非常简单的长时间运行的任务作为 AsyncTaskLoader: public class DumbLoader extends AsyncTaskLoader{ private s
我有以下代码在异步任务中运行客户端套接字连接: @Override protected Boolean doInBackground(Void... params) { //This runs on
我已经多次使用 AsyncTask - 但我遇到了一个看似简单的问题却让我感到困惑。问题是这样的: Is publishProgress(Progress... values) supposed to
我的实际任务正在完成,但是即使我调用进度对话框,我的进度条的进度状态也没有任何增加..?有人能告诉我哪里做错了吗? class A extends AsyncTask
我试图在后台进程中发布两种不同的进度。我试图有时发布一个字符串,另一次发布一个整数。我也通过重载处理 onProgressUpdate 中的两种参数。但是当我声明我的 AsyncTask 类时,我有参
由于垃圾收集器被调用了很多次,我一直在寻找一种在循环语句中 x 秒后调用 publishProgress() 的方法。 这是我的: protected Void doInBackground(Void
我已经查看过有关此问题的所有类似线程,但找不到解决我的问题的方法 我的 AsynkTask 应该从保管箱下载文件并在通知进度条中显示进度。 doInBackground 下载文件,但方法 publis
我正在尝试实现简单的内部数据存储代码。我想在其中显示一些后台进程的 ProgressDialog,在调用 publishProgress() 时该进程将递增 5。但它给我如下错误, The metho
我已经阅读了这里几乎所有关于使用 publishProgress 在进度对话框上设置新消息的帖子,但我就是无法让它工作。 代码: public class LoginToApi extends Asy
我解析 XML 数据并将其放入一个对象中。这需要相当长的时间,我决定使用 AsyncTask 在后台运行它。我的代码几乎与这个例子完全一样: How to get XML using AsyncTas
我的 AsyncTask 看起来像: private class MyTask extends AsyncTask> { private volatile UserData userData;
我在这上面花了太多时间,到目前为止,我一直没有使用 publishProgress() 功能……幸运的是,onPostExecute 也在 UI 线程中运行,所以我不得不为我想要的每个图像创建 N 个
Developer.Android 说 PublishProgress 驻留在 UI 线程中,因此我应该能够从 Publish Progress 更改 UI。 但是,我得到这个错误: android.
我是一名优秀的程序员,十分优秀!