开发者

Android Asynctask and progressDialog

What I would like to do is give my app the ability to download my mp3 of my server. So far I have the download mp3 into a audio file working but it's very finicky and cannot be disturbed in order for it to work properly. That being said I would love to have a progress dialog pop up that cannot be canceled so the user can't interrupt the progress while downloading the file to the folder in the background. After reading it seemed that AsyncTask would be the best way to do this but I cannot get it to work. Below is one of the buttons from my code.

public class music extends Activity {

    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


   开发者_StackOverflow社区        // progressDialog.dismiss();
            fileOutput.close();

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

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

        e.printStackTrace();
    }

    return null;
    }

    }

}

So if I stripped out all the code dealing with asynctask it works it's just extremely un-user friendly but the files do download. When I try to add the progress dialog and background task it quits on me. I have a feeling it has to do with the parameters.


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();
    }
}

Try this, a alternate way to show Dialog


Just a quick scan, I don't think you should be calling msg.show(); from the background thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜