protected String doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
URLConnection connection = url.openConnection();
connection.connect();
//Detect File Length
int fileLength = connection.getContentLength();
//Locate Storage Location
String filePath = Environment.getExternalStorageDirectory().getPath();
//Downloading File
final InputStream input = new BufferedInputStream(url.openStream());
//Save the Download File
OutputStream output = new FileOutputStream(filePath + "/" + "Ardu_Vid.mp4");
final byte data[] = new byte[fileLength];
long total = 0;
int count;
long Start_Timee = System.currentTimeMillis();
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
//publish the progress
publishProgress((int) (total * 100/fileLength));
Long End_Timee = System.currentTimeMillis();
Log.e("Time_End", "" + End_Timee);
final long timeDiff = End_Timee - Start_Timee;
/************ Get Duration of Downloading ***************/
Long allTimeForDownloading = (timeDiff * fileLength/total);
Long remainingTime = allTimeForDownloading - timeDiff;
int seconds = (int) (remainingTime/1000) % 60 ;
int minutes = (int) ((remainingTime/(1000*60)) % 60);
Log.e("elapse",""+minutes+":"+seconds);
//Problem is here
textView.setText(""+minutes +" : "+ seconds);
}// end while
我的編碼工作正常,但是當我添加textView.setText
打印剩餘下載時間提交其無法正常工作的問題是我被甩一次罰球循環,但我的文本視圖不會被動態地設置時間當我在日誌中打印時,我會得到準確的結果。請幫幫我。集的TextView在while循環打印時間
你不應該在線程中執行UI操作 –
我已經完成了我的任務......沒有加入 –
如果你的後臺操作正在做一些繁重的工作,並且你希望你的UI被同時更新,那麼你應該考慮使用線程或Android具有內置線程的AsyncTask。這樣你的UI不會被阻塞。 –