我尝试通过AsyncTask设置进度条。但我有一点问题。
我的代码在这里;
'[["a", 1], ["b", 2], ["c", 3]]'
Android工作室说:必须从UI线程调用方法 public class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected String doInBackground(String... f_url) {
try {
//CreateDownloadTransactionInPanel(1);
long total = 0;
URL url = new URL(f_url[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(file.exists()){
//6829809
deneme = file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-"); // indirmemiÅŸse atamaz
}
connection.connect();
RandomAccessFile output = new RandomAccessFile(file,"rw");
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
if (file.length() > 0) {
output.seek(deneme);
total=deneme;
}
byte data[] = new byte[4096];
int lastcount = 0;
while ((count = input.read(data,0,4096)) != -1) {
if (isCanceled) {
downloadresult = false;
break;
}
if (intCheck()) {
total += count;
downloadresult = true;
ProgBarCount = (int) ((total * 100) / lenghtOfFile);
if (ProgBarCount > lastcount) {
lastcount = ProgBarCount;
publishProgress(Integer.toString(ProgBarCount));
pBar.setProgress(ProgBarCount);
}
output.write(data, 0, count);
}
else {
downloadresult = false;
break;
}
} //--While Son--
output.close();
input.close();
}
catch (Exception e) {
e.printStackTrace();
exmessage = e.getMessage().toString();
downloadresult = false;
}
return null;
} // doinBackSON
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
mBuilder.setProgress(100, Integer.parseInt(progress[0]), false);
mBuilder.setContentText("Şuan İşlemde : %" + Integer.parseInt(progress[0]) + "");
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String downloadpath) {
if (exmessage != null) {
Toast.makeText(getApplicationContext(),"hata:"+exmessage, Toast.LENGTH_SHORT).show();
}
if (downloadresult == true) {
if (file.exists() && lenghtOfFile == (int) file.length()) {
Toast.makeText(DownloadActivity.this, "DOSYA INDIRILDI VE YUKLEMEYE HAZIR", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "DOSYA YOK YADA EKSIK INDIRILDI", Toast.LENGTH_SHORT).show();
}
CreateDownloadTransactionInPanel(2);
scheduleNotificationWithTickButton(); // Tick Button
RedirectRebootActivity(); // Burda hata olabilir
} //--True Son
else // False Başlangıç
{
mNotifyManager.cancel(2219);
//scheduleNotificationWithResumeButton(); Devam button
}
}
}
。
这一行setProgress
有可能吗? ,你对此有任何想法吗?
谢谢!
答案 0 :(得分:2)
您致电PublishProgress
这将触发对AsyncTask中的onProgressUpdate方法的调用
@Override
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
在此方法中进行UI操作
答案 1 :(得分:-2)
@Overide
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
你可以用这个