强制关闭,使用asynctask下载文件
我有一个正在运行的下载功能。但是当我运行它时,如80%的时间让我的手机变得迟缓,强行关闭,而不是像1〜2分钟那样长时间响应。这个案件发生得非常随机,我真的无法追查到底是什么问题。下载完成后,设备将恢复正常。我曾尝试过各种设备,如星系S2,星系笔记,SE xperia Arc S和几张桌子。问题依然存在。任何人都可以告诉我如何提高我的代码?下面是我现有的代码:强制关闭,使用asynctask下载文件
public void onClickDownload(View view){
String url = "http://www.mydomain.com./" + fileURL;
url = url.replaceAll(" ","%20");
String sourceUrl = url;
new DownloadFileAsync().execute(sourceUrl);
}
public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {
private boolean run_do_in_background = true;
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lengthOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile);
File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
boolean success = false;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (!success) {
} else {
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/MaxApps/" + apkURL);
byte data[] = new byte[100*1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
int progressPercent = (int) ((total*100)/lengthOfFile);
if(progressPercent % 5 == 0){
publishProgress(progressPercent);
}
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
notificationManager.cancel(Integer.parseInt(ID.toString()));
Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
MyN.tickerText = "Download Failed";
MyN.number = 1;
MyN.setLatestEventInfo (getApplicationContext(), apkURL + " Download Failed.", "Please try again", MyPI);
MyN.flags |= Notification.FLAG_AUTO_CANCEL;
MyNM.notify(1, MyN);
run_do_in_background = false;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
notificationManager.notify(Integer.parseInt(ID.toString()), notification);
}
@Override
protected void onPostExecute(Void unused) {
if(run_do_in_background) {
notificationManager.cancel(Integer.parseInt(ID.toString()));
Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
MyN.tickerText = "Download Complete";
MyN.number = 1;
MyN.setLatestEventInfo (getApplicationContext(), "Download Complete, Click to install.", apkURL, MyPI);
MyN.flags |= Notification.FLAG_AUTO_CANCEL;
MyNM.notify(Integer.parseInt(ID.toString()) , MyN);
}
}
}
这可能是更新的UI花了很长时间?也许你可以尝试测试这样,看看它是否有差别:
@Override
protected void onProgressUpdate(Integer... progress) {
Log.d("ANDRO_ASYNC", "Progress: " + progress[0]);
}
顺便说一句,这是不相关的,你问什么,但我认为你有一个处理的进展情况在你的代码的问题:
int progressPercent = (int) ((total*100)/lengthOfFile);
if(progressPercent % 5 == 0){
publishProgress(progressPercent);
}
,只有当它是完全5,10,15,20%等将更新的进展...我猜你真正想要更新的进展时,它至少是5%,进一步比以前。
int previousProgress = 0;
while ((count = input.read(data)) != -1) {
total += count;
int progressPercent = (int) ((total*100)/lengthOfFile);
if(progressPercent - previousProgress >= 5) {
previousProgress = progressPercent;
publishProgress(progressPercent);
}
output.write(data, 0, count);
}
感谢您的回复,但我有一些问题:%5与> = 5之间的差异实际上是什么?他们只是做同样的事情吗?你提供的新计算看起来像是让系统做更多的工作? – melvintcs 2012-07-05 10:34:00
假设进度是不变的,每次都是%6。所以它会像这样:6,12,18,24,30,...你的代码只有在%30%,60%,90%时才会更新UI。我的建议会每次更新它,是的它是更多的工作。但我只是认为你的代码没有做你打算做的事情。那个建议和你的问题没有关系.. – Caner 2012-07-05 10:36:49
ic,从来没有想过它。 thx为解释:) – melvintcs 2012-07-05 10:46:10
我不太喜欢你更新onProgressUpdate中的通知,因为你在UI线程中执行这些操作,然后你以某种方式阻止它。 – rciovati 2012-07-05 10:00:28
你如何创建并执行'DownloadFileAsync'对象? – Caner 2012-07-05 10:04:53
是的,我确实为通知栏创建了一个新的用户界面以满足我的需要,那我该怎么办?这是导致问题的原因吗? @Caner,检查更新:) – melvintcs 2012-07-05 10:05:07