ich auf einem Android-Anwendung arbeite, hier mit einem ProgressBar Ich arbeite, für die ich runOnUiThread()
bin mit visible/GONE von doInBackground()
Methode von AsyncTask zu machen, habe ich nicht finden andere Orte außer doInBackground()
geeignet, um VISIBLE/GONE ProgressBar die UI-Anforderung zu erfüllen.ProgressBar dreht sich nicht - Android
In diesem Kodierungsszenario ist ProgressBar sichtbar, aber nicht rotierend, Es rotiert, wenn ich ProgressBar von onCreate()
Methode der Aktivität sichtbar mache.
Ich verstehe nicht, warum es passiert, ich besuchte einige Beiträge auf Stackoveflow, sie sagen, dass UI-Thread ProgressBar nicht drehen kann, da es bei der Verarbeitung anderer Aufgaben zur Verfügung steht, damit ich es nicht finden kann andere Möglichkeit, es zum Laufen zu bringen.
Also bitte helfen Sie mir, was sollte die Behebung dieses Problems sein. Hier ist der Code, den ich benutze, um VISIBLE/GONE ProgressBar zu machen.
@Override
protected String doInBackground(String... f_url) {
int count;
try {
zipfileName = f_url[1];
fUrl3 = f_url[2];
if (fUrl3.equals("DontDownloadZip")) {
return "DownloadSuccess";
} else {
Log.d("Downloading ", zipfileName);
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Hide ProgressBar after server connection .
runOnUiThread(new Runnable() {
@Override
public void run() {
tipLayout.setVisibility(GONE);
}
});
// Code to download a file .
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(MyBooks.screen
.getFilesDir().getAbsolutePath()
+ "/BookZips/"
+ f_url[1]);
byte data[] = new byte[1024];
long total = 0;
int counter = 0;
while ((count = input.read(data)) != -1) {
counter++;
total += count;
// Display ProgressBar when percentage is 99 .
if ((int) ((total * 100)/lenghtOfFile) == 99) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!identifier1.equals("BookZips9Download"))
tipLayout.setVisibility(VISIBLE);
}
});
}
publishProgress("" + (int) ((total * 100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
android.os.FileUtils.setPermissions(MyBooks.screen
.getFilesDir().getAbsolutePath()
+ "/BookZips/"
+ f_url[1], FileUtils.S_IRWXU | FileUtils.S_IRWXG
| FileUtils.S_IROTH | FileUtils.S_IWOTH
| FileUtils.S_IXOTH, -1, -1);
}
} catch (Exception e) {
e.printStackTrace();
return "DownloadFailed";
}
return "DownloadSuccess";
}
Sie können sichtbar in OnPreExecute() -Methode von AsyncTask. –