Reputation: 15718
Boolean timer = false;
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
downloadmanagerstart();
System.out.println("return");
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(download_id);
Cursor cursor = downloadManager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor
.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor
.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
cursor.close();
publishProgress(bytes_downloaded, bytes_total);
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
final int bytes_downloaded = values[0];
System.out.println(bytes_downloaded
+ " Downloaded 1");
int bytes_total = values[1];
int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
String totaldownload_str = android.text.format.Formatter
.formatFileSize(MainActivity.this, bytes_downloaded);
current_tvm.setText(totaldownload_str);
mProgressBar.setProgress((int) dl_progress);
if (!timer) {
Thread thread = new Thread() {
@Override
public void run() {
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
System.out.println(bytes_downloaded
+ " Downloaded 2");
}
});
}
}, 0, 1000);
}
};
thread.start();
timer = true;
}
}
As you can see I have put two System.out.println(bytes_downloaded+ " Downloaded 1");
in onProgress andSystem.out.println(bytes_downloaded+ " Downloaded 2");
inside a thread on onProgress. The vale of bytes_downloaded
is always 0 inside the thread why is this happening. I wanted it to be sync every second with the value bytes_downloaded
in onProgress. thanks in advance.
Upvotes: 0
Views: 58
Reputation: 93614
Its 0 because its a final variable. Once its set, it can't change. So it will always be what you initially set it to. If you want to pass data like that use a member variable of the class (with synchronization if necessary), not a final or method level variable.
Additionally, this approach isn't needed. onProgressUpdate is already run on the UI thread- that's the point of the function. Why would you want to delay the UI update more, and why would you use such a convoluted way of doing so? I think you really don't understand the concepts of how threads, timers, and AsyncTasks work, because this code makes 0 sense.
Upvotes: 1