2013-03-07 52 views
-1

由於狀態,我需要使用AsyncTask下載兩個文件。當我嘗試時,沒有下載任何東西。如果需要,我可以給一個logcat。我不知道爲什麼它不會下載這些文件。這裏是我的代碼:Android-無法獲取AysncTask下載多個文件

package com.cydeon.smirkit; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.concurrent.TimeoutException; 

import com.stericson.RootTools.*; 
import com.stericson.RootTools.exceptions.RootDeniedException; 
import com.stericson.RootTools.execution.CommandCapture; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class Stock extends Activity implements OnClickListener { 

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private ProgressDialog mProgressDialog; 

private String urlPath = "https://dl.dropbox.com/s/";  
private String[] fileNames = {"u-boot_stock.bin","u-boot_stock.md5"}; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.stock); 
    Button bInstallS = (Button) findViewById(R.id.bInstallS); 
    Button bSkipS = (Button) findViewById(R.id.bSkipS); 
    bInstallS.setOnClickListener(this); 
    bSkipS.setOnClickListener(this); 
    mProgressDialog = new ProgressDialog(Stock.this); 
    mProgressDialog.setMessage("Downloading..."); 
    mProgressDialog.setIndeterminate(false); 
    mProgressDialog.setMax(100); 
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
} 
     public void onClick(View arg0) { 
      switch(arg0.getId()){ 
       case R.id.bInstallS: 
        AlertDialog.Builder builder = new AlertDialog.Builder(Stock.this); 
        builder.setPositiveButton(R.string.continueStock, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          startDownload(); 
         } 
         }); 
        builder.setNegativeButton(R.string.cancelStock, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          Stock.this.finish(); 
         } 
         }); 
        builder.setMessage(R.string.messageStock); 
        builder.setTitle(R.string.titleStock); 
        AlertDialog dialog = builder.create(); 
        dialog.show(); 



        break; 
       case R.id.bSkipS: 
        Intent skip = new Intent(Stock.this, Twrp.class); 
        startActivity(skip); 

     } 
     } 

private void startDownload() {  

    if(checkExternalMedia()==true) { 

      File file = null;     
      for(int i=0; i<fileNames.length; i++) { 
       file = new File(Environment.getExternalStorageDirectory()+fileNames[i]); 
       boolean exists = file.exists(); 
       if(exists){ 
        continue; 
       } 
       else { 
        new DownloadFileAsync().execute(urlPath+"2mtbqt7mk5khqu3/"+fileNames[i],"klw4vb0valwds2p/"+fileNames[i]);    
       } 
       file = null; 
      }    
     }    
    else {   
    } 
} 
/** Method to check whether external media available and writable. */ 

private boolean checkExternalMedia(){ 
    boolean mExternalStorageAvailable = false; 
    boolean mExternalStorageWriteable = false; 
    boolean stat; 
    String state = Environment.getExternalStorageState(); 

    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     // Can read and write the media 
     mExternalStorageAvailable = mExternalStorageWriteable = true; 
     stat = true; 
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     // Can only read the media 
     mExternalStorageAvailable = true; 
     mExternalStorageWriteable = false; 
     stat = false; 
    } else { 
     // Can't read or write 
     mExternalStorageAvailable = mExternalStorageWriteable = false; 
     stat = false; 
    } 

    return stat; 
} 

class DownloadFileAsync extends AsyncTask<String, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog.show(); 
    } 

    protected String doInBackground(String... sURL) { 
     try{ 
      URL url = new URL(sURL[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      //Shows 0-100% progress bar 
      int fileLength = connection.getContentLength(); 

      //Download the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/"+fileNames); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       //Publish the Progress 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
       } 

      output.flush(); 
      output.close(); 
      input.close(); 
    } catch (Exception e) { 

    } 
    return null; 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     mProgressDialog.dismiss(); 
     Context context = getApplicationContext(); 
     CharSequence text = "Installing. Please Wait"; 
     int duration = Toast.LENGTH_SHORT; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     RootTools.remount("/system", "rw"); 
     CommandCapture command = new CommandCapture(0, "su"); 
     try { 
      RootTools.getShell(true).add(command).waitForFinish(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (TimeoutException e) { 
      e.printStackTrace(); 
     } catch (RootDeniedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


} 

因此,如果需要,我可以再提供logcat。這是我第一次嘗試這個,所以我不知道我在做什麼..

+2

你有沒有調試過?如果有任何相關的logcat消息,你應該發佈它們 – dymmeh 2013-03-07 20:08:28

回答

0

如果我理解正確,你不想以相同的時間(同時)下載所有的文件,但一個一個(連續)。爲了做到這一點,建立一個帶有要下載的URL的字符串數組,並用該數組調用execute()。