2011-07-29 90 views
2

我想要做的是让我的应用程序能够下载我的服务器的mp3。到目前为止,我已经将下载的mp3下载到音频文件中,但它非常挑剔,为了正常工作,不会受到干扰。这就是说,我很想有一个进度对话框弹出不能被取消,因此用户不能中断进度,而将文件下载到后台文件夹。在阅读之后,似乎AsyncTask会是这样做的最好方式,但我无法实现它。下面是我的代码中的一个按钮。Android Asynctask and progressDialog

公共类音乐延伸活动{

public static int mProgress = 0; 
    static String filename; 
    MediaPlayer buttonclicker; 
    static Toast msg; 
    public static int totalSize = 0; 
    public ProgressDialog dialog; 
    public static boolean isFinished; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.music); 
    buttonclicker = MediaPlayer.create(this, R.raw.button); 
    Button boomFullDownload = (Button) findViewById(R.id.boomfull); 

boomFullDownload.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      buttonclicker.start(); 
      filename = "boomboom.mp3"; 
      new downloadPumphouseShow().execute(filename); 

}

class downloadPumphouseShow extends AsyncTask<String , Void, Void> { 

    ProgressDialog dialog; 

    Toast msg; 

    protected void onPreExecute(){ 
     dialog = new ProgressDialog(context); 
     msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG); 
     msg.setGravity(Gravity.CENTER, msg.getXOffset()/2, msg.getYOffset()/2); 
     dialog.setMessage("Please Wait Loading"); 
     dialog.setCancelable(false); 
     dialog.show(); 

    } 
} 
    }); 

    protected void onPostExecute(Void result) { 
     dialog.hide(); 
     dialog.dismiss(); 
     } 


    protected Void doInBackground(String... params) { 

     String filename = params[0]; 

    try { 

      //set the download URL, a url that points to a file on the internet 
      //this is the file to be downloaded 
      URL url = new URL("http://lepumphouse.com/media/" + filename); 

      //create the new connection 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      //set up some things on the connection 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 

      //and connect! 
      urlConnection.connect(); 

      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      File Music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/Pumphouse/Party Cake"); 

     //create a new file, specifying the path, and the filename 
      if(Music.exists()) 
       msg.show(); 
      else 
       Music.mkdirs(); 
      //which we want to save the file as. 
      File file = new File(Music, filename); 

      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //this will be used in reading the data from the internet 
      InputStream inputStream = urlConnection.getInputStream(); 

      //this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      //variable to store total downloaded bytes 
      int mProgress = 0; 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //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 
        mProgress += bufferLength; 
        //this is where you would do something to report the pr0gress, like this maybe 


      } 
      //close the output stream when done 


      // progressDialog.dismiss(); 
      fileOutput.close(); 

    //catch some possible errors... 
    } catch (MalformedURLException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

    return null; 
    } 

    } 

}

所以,如果我去掉了所有处理的AsyncTask它的工作原理,它只是极度非人性化的代码但是这些文件会下载。当我尝试添加进度对话框和后台任务时,它会退出我。我有一种感觉,它与参数有关。

+1

什么是错误?请发布从日志中出现的错误。 – ayublin

+0

看AsycnTask DOC:http://developer.android.com/reference/android/os/AsyncTask.html如果你想显示你应该使用onProgressUpdate,因为它是示例中显示进度。 – ania

回答

2
protected void onPreExecute() { 
    dialog=ProgressDialog.show(mContext, "", "Fetching book oversight"); 
    msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG).show; 
    super.onPreExecute(); 
} 



protected void onPostExecute(Void result) { 
    if(dialog!=null) 
    { 
     dialog.dismiss(); 
    } 
} 

试试这个,一个替代的方式来显示对话框

2

只是一个快速扫描,我不认为你应该调用msg.show();从后台线程。