使用Progressbar

时间:2013-03-20 06:15:24

标签: android progress-bar

我创建了一个应用程序,其中有5个按钮用于流式传输5个不同的直播频道。除了5个按钮,我还有一个进度条(环)。 Iam使用进度条,因为视频需要时间来加载。我已经在AsyncTask的onBackground()中编写了主代码,它返回了一些在按钮的click事件中调用的值。由于所有5个按钮都分配了不同的url,如何为所有5个按钮使用相同的onBackground()?在这种情况下该怎么办?任何人都可以给我一个很好的例子。

2 个答案:

答案 0 :(得分:0)

onBackground()是AsyncTask的一部分。它与按钮或其他UI元素没有任何共同之处。如果要重用代码,只需将UI作为参数传递给AsyncTask构造函数,然后在需要时使用它。

答案 1 :(得分:0)

Video Path

私有静态字符串file_url =“url”;

  in your activity

new DownloadFileFromURL()。execute(file_url);

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case progress_bar_type: // we set this to 0
        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;
    default:
        return null;
    }
}

Background Async Task to download file



/**
     * Background Async Task to download file
     * */
    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();

                // this will be useful so that you can show a tipical 0-100%
                // progress bar
                int lenghtOfFile = conection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream(),
                        8192);

                // Output stream
                OutputStream output = new FileOutputStream(Environment
                        .getExternalStorageDirectory().toString()
                        + "/demo.mp4");

                byte data[] = new byte[1024];

                long total = 0;

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

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);

            // Displaying downloaded video into video view
            // Reading image path from sdcard
            String videopath = Environment.getExternalStorageDirectory()
                    .toString() + "/demo.mp4";
            // setting downloaded into image view

        }

    }