AsyncTask
kullanıyorum
, şu anda indirme ilerleme olarak aşağıda onProgressUpdate
olarak güncellenen bir ProgressDialog
olarak gösterilir: Bu şimdi ancak, iyi çalışıyorDosya indirilirken Bildirim güncellenirken UI gecikmesi nasıl önlenir?
protected String doInBackground(String... sUrl) {
try {
String destName = sUrl[1];
file_Delete(destName); // Just to make sure!
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e(TAG, NAME + ": Error downloading file! " + e.getMessage());
return e.getMessage();
}
return null;
}
@Override protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
DownloadImage.mProgressDialog.setProgress(progress[0]);
}
bildirim çubuğunda bir bildirim kullanmak istiyorsanız, indirme işlemini takip edin (dosya oldukça büyük olabilir ve kullanıcılar uygulamanın dışından takip etmek isterler).
Ben aşağıdaki kodu denedim ancak kullanıcı arayüzüne çok adlandırılan alma publishProgress
onun yüzünden görebilirsiniz kötü gecikme başlar, böylece nasıl publishProgress
sadece her saniye
@Override protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
DownloadImage.mProgressDialog.setProgress(progress[0]);
DownloadImage.myNotification = new NotificationCompat.Builder(c)
.setContentTitle("Downloading SlapOS")
.setContentText("Download is " + progress[0] + "% done")
.setTicker("Downloading...")
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setProgress(100, progress[0], false)
.setSmallIcon(R.drawable.icon)
.build();
DownloadImage.notificationManager.notify(1, DownloadImage.myNotification);
}
çağırmak için arka plan kodu değiştirme hakkında gidebiliriz
Şu anda hangi sürümü çalıştırıyorsunuz? – JPM
Min SDK sürümü 8, ancak 4.2.1 çalıştıran bir aygıtta ve her ikisi de aynı sonuçla 2.3.4 çalıştıran bir aygıt üzerinde test ettim (sistemUI çöktüğünde ve yeniden başlatılana kadar UI söner) –