【问题标题】:Android Asynctask and progressDialogAndroid Asynctask 和 progressDialog
【发布时间】:2011-07-29 01:24:23
【问题描述】:

我想做的是让我的应用能够下载我的服务器的 mp3。到目前为止,我已将 mp3 下载到音频文件中,但它非常挑剔,不能被打扰以使其正常工作。话虽如此,我希望弹出一个无法取消的进度对话框,因此用户在将文件下载到后台文件夹时不能中断进度。阅读后似乎 AsyncTask 将是执行此操作的最佳方法,但我无法使其正常工作。下面是我的代码中的一个按钮。

公开课音乐扩展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


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

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

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

        e.printStackTrace();
    }

    return null;
    }

    }

}

因此,如果我去掉所有处理 asynctask 的代码,它就可以工作,这对用户来说非常不友好,但文件确实可以下载。当我尝试添加进度对话框和后台任务时,它退出了我。我感觉它与参数有关。

【问题讨论】:

标签: java android android-asynctask progressdialog


【解决方案1】:
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();来自后台线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 2014-12-18
      • 1970-01-01
      • 2018-01-28
      • 2014-05-29
      • 2017-10-24
      • 1970-01-01
      相关资源
      最近更新 更多