HY!我有一个asynctask下载3文件frm互联网。 在下载中,我正在显示进度条。 现在我把这个asynctask放到另一个,它会产生一个错误。 无法在没有调用Looper.prepare()
的线程中创建处理程序但是如果我在“预先执行”下载工作时调用Looper.prepare(),但我没有看到对话框。 想法为什么?
这是异步方法的代码
private class DownloadFilesTask extends AsyncTask<String, Integer, String> {
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Mostro la dialog");
Looper.prepare();
showDialog(1);
Toast.makeText(getApplicationContext(), "Download file in corso. il player verrà aperto al termine", Toast.LENGTH_LONG).show();
System.out.println("Mostrata");
pDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
//DownloadFilesTask().cancel(true);
System.out.println("Ho chiuso la finestra durante il download dei file");
myTask.cancel(true);
}
});
pDialog.setProgress(0);
}
protected String doInBackground(String... string) {
int count;
int lenghtOfFile = 0;
try {
URL url;
URLConnection conection;
long total = 0;
if(fi3!=0){
url = new URL(string[2]);
conection = url.openConnection();
conection.connect();
lenghtOfFile += conection.getContentLength();
}
if(fi2!=0){
url = new URL(string[1]);
conection = url.openConnection();
conection.connect();
lenghtOfFile += conection.getContentLength();
}
if(fi1!=0){
url = new URL(string[0]);
conection = url.openConnection();
conection.connect();
//download primo file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"mp3 Scaricati");
if(!cacheDir.exists())
cacheDir.mkdirs();
File f=new File(cacheDir,file1);
// Output stream to write file
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
output.flush();
// closing streams
output.close();
input.close();
if(isCancelled()) return null;
}else{
System.out.println("Il file " + file1 + " è già presente");
}
//download secondo file
if (fi2!=0) {
System.out.println("Il file non esiste: " +file2);
url = new URL(string[1]);
conection = url.openConnection();
conection.connect();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"mp3 Scaricati");
if(!cacheDir.exists())
cacheDir.mkdirs();
File f=new File(cacheDir,file2);
OutputStream output = new FileOutputStream(f);
byte data1[] = new byte[1024];
while ((count = input.read(data1)) != -1) {
total += count;
publishProgress((int)((total*100)/lenghtOfFile));
output.write(data1, 0, count);
}
output.flush();
// closing streams
output.close();
input.close();
if(isCancelled()) return null;
}else{
System.out.println(file2 + "già presente");
}
//Dowload terzo file
if (fi3!=0) {
System.out.println("Il file non esiste: " +file3);
url = new URL(string[2]);
conection = url.openConnection();
conection.connect();
// getting file length
// lenghtOfFile = conection.getContentLength();
//Fine calcolo dimensione file
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"mp3 Scaricati");
if(!cacheDir.exists())
cacheDir.mkdirs();
File f=new File(cacheDir,file3);
// Output stream to write file
OutputStream output = new FileOutputStream(f);
byte datan[] = new byte[1024];
while ((count = input.read(datan)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int)((total*100)/lenghtOfFile));
// writing data to file
output.write(datan, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} else{
System.out.println(file3 + "già presente");
}
if(isCancelled()) return null;
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
if (isCancelled()){ return;}
else{ pDialog.setProgress(progress[0]);
}
}
protected void onPostExecute(String result) {
pDialog.dismiss();
//Avvio il player
Toast.makeText(getApplicationContext(), "Download completo, avvio il player", Toast.LENGTH_LONG).show();
startActivity(StartPlayer);
}
}
protected Dialog onCreateDialog(int id) {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
}