Im пытается установить индикатор прогресса через AsyncTask. Но у меня есть небольшая проблема.Метод setProgress должен вызываться из потока пользовательского интерфейса?
Мой код здесь;
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
}
}
}
Android студии говорят: этот метод setProgress
должен вызываться из потока пользовательского интерфейса.
эта линия pBar.setProgress(ProgBarCount);
это возможно? , У вас есть представление об этом?
Спасибо!
Спасибо брат! он работает <3 –
Добро пожаловать – Kuffs