问题描述
我试图使用的AsyncTask
来下载文件与通知的进展。一切工作正常(下载在后台线程),但每当我添加code ,它冻结了整个手机更新通过进度条publishProgress(),直到下载完成,并通知显示下载完成。
我完全失去了,为什么是这样的话,但我也许想这是沿着 publishProgress的线((downloadedSize / totalSize)* 100)
我使用?
反正这里是 DownloadDataTask
:
保护字符串doInBackground(RomDataSet ... PARAMS){
尝试 {
//此处下载文件时,都好
//现在,读取通过输入缓冲器和将内容写入到该文件
而(量(bufferLength = inputStream.read(缓冲液))大于0){
//在缓冲器的数据添加到该文件中的文件输出流(该文件的SD卡上
fileOutput.write(缓冲液,0,BufferLength中);
//加起来的规模,所以我们知道有多少下载
downloadedSize + = BufferLength中;
//这是在那里你会做一些报告prgress,这样也许
publishProgress((downloadedSize / totalSize)* 100);
}
完成后//关闭输出流
fileOutput.close();
//捕捉一些可能出现的错误?
}赶上(MalformedURLException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
返回null;
}
@覆盖
在preExecute保护无效(){
意向意图=新的意图(ListActivity.this,ListActivity.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,意图,0);
//配置通知
通知=新的通知(R.drawable.ic_stat_rom,通过RomGet下载连不上,系统
.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView =新RemoteViews(getApplicationContext()getPackageName(),R.layout.layout_download。);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon,R.drawable.icon_rom);
notification.contentView.setTextViewText(R.id.download_description,下载);
notification.contentView.setProgressBar(R.id.download_progress,100,0,假);
getApplicationContext();
notificationManager =(NotificationManager)getApplicationContext()。getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(43,通知);
}
@覆盖
保护无效onProgressUpdate(整数...进度){
//notification.contentView.setProgressBar(R.id.download_progress,100,Math.round(进展[0]),FALSE);
notification.contentView.setTextViewText(R.id.download_description,Integer.toString(进展[0]));
//告知进度更新的进度条
notificationManager.notify(43,通知);
}
@覆盖
保护无效onPostExecute(字符串结果){
notification.contentView.setProgressBar(R.id.download_progress,100,100,FALSE);
notification.contentView.setTextViewText(R.id.download_description,完成);
notificationManager.notify(43,通知);
}
我真的卡在这一个 - 任何帮助将是AP preciated。干杯
@覆盖
保护无效onProgressUpdate(整数...进度){
如果((进展[0] - lastSize)GT; 5000){
lastSize =进展[0];
notification.contentView.setProgressBar(R.id.download_progress,totalSize,进步[0],假);
//notification.contentView.setTextViewText(R.id.download_description,Integer.toString(进展[0]));
//告知进度更新的进度条
notificationManager.notify(43,通知);
}
}
使用一个mod运算符,只更新5,10%或20%。你打电话onProgressUpdate()不断地在下载过程中。
如果(downloadedSize%(totalSize / 20)== 0){// 20更新每隔5%,10每10%和5每隔20%,等等。
publishProgress((downloadedSize / totalSize)* 100);
}
I'm trying to use an AsyncTask
to download a file with a notification progress. Everything works fine (downloads in background thread), except whenever I add the code to update the progress bar through publishProgress()
, it freezes the whole phone, until the download is finished and the notification shows "Download Completed".
I'm completely lost as to why this is the case, but I am maybe thinking it's along the lines of the publishProgress((downloadedSize / totalSize) * 100)
that i'm using?
Anyways here is the DownloadDataTask
:
protected String doInBackground(RomDataSet... params) {
try {
// Download file here, all good
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
publishProgress((downloadedSize / totalSize) * 100);
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
Intent intent = new Intent(ListActivity.this, ListActivity.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
// configure the notification
notification = new Notification(R.drawable.ic_stat_rom, "Downloading Rom via RomGet", System
.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.layout_download);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon_rom);
notification.contentView.setTextViewText(R.id.download_description, "Downloading");
notification.contentView.setProgressBar(R.id.download_progress, 100, 0, false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(43, notification);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//notification.contentView.setProgressBar(R.id.download_progress, 100, Math.round(progress[0]), false);
notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
// inform the progress bar of updates in progress
notificationManager.notify(43, notification);
}
@Override
protected void onPostExecute(String result) {
notification.contentView.setProgressBar(R.id.download_progress, 100, 100, false);
notification.contentView.setTextViewText(R.id.download_description, "Done");
notificationManager.notify(43, notification);
}
I'm really stuck on this one - Any help would be appreciated. Cheers
@Override
protected void onProgressUpdate(Integer... progress) {
if ((progress[0] - lastSize) > 5000) {
lastSize = progress[0];
notification.contentView.setProgressBar(R.id.download_progress, totalSize, progress[0], false);
//notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
// inform the progress bar of updates in progress
notificationManager.notify(43, notification);
}
}
Use a mod operator to only update on 5, 10 or 20 %. You're calling onProgressUpdate() constantly during the download.
if (downloadedSize % (totalSize / 20) == 0) { // 20 updates every 5%, 10 every 10% and 5 every 20%, etc.
publishProgress((downloadedSize / totalSize) * 100);
}
这篇关于冻结UI线程使用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!