如何在下载时显示进度条?

时间:2016-12-05 00:23:03

标签: android android-progressbar android-download-manager

我想使用android downloadManager从URL下载文件,并使用进度条显示下载进度。

xml文件:

 <ProgressBar
                        android:id="@+id/progress_bar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:layout_alignParentBottom="true"
                        android:progressTint="@color/downloadColorDark"
                        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"/>

下载片段代码:

Button buttonDownload;
DownloadManager downloadManager;
protected ProgressBar mProgressBar;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_download, container, false);
    buttonDownload = (Button)view.findViewById(R.id.downloadMusicButton);
    buttonDownload.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            downloadManager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
            Uri uri = Uri.parse("http://org3.s1.mp3.zdn.vn/871a49ac2fe8c6b69ff9/6036763354470939079?key=r-YWGIWypTz_FZeaVGri_Q&expires=1480915451&filename=Iridescent%20Linkin%20Park%20Linkin%20Park%20-%20Linkin%20Park.mp3");
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            final long downloadId = downloadManager.enqueue(request);

            mProgressBar = (ProgressBar)view.findViewById(R.id.progress_bar);
            Timer myTimer = new Timer();
            myTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(downloadId);
                    Cursor cursor = downloadManager.query(q);
                    cursor.moveToFirst();
                    int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                    cursor.close();
                    final int dl_progress = (bytes_downloaded * 100 / bytes_total);
                    getActivity().runOnUiThread(new Runnable(){
                        @Override
                        public void run(){
                            mProgressBar.setProgress(dl_progress);
                        }
                    });

                }

            }, 0, 10);
        }
    });

    return view;
}

下载功能完美无缺,但在我添加了进度条的代码后,当我按下载按钮时,应用程序崩溃了。请告诉我我做错了什么。

错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: vn.edu.usth.musicplayer, PID: 4456
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setProgress(int)' on a null object reference
                  at vn.edu.usth.musicplayer.fragment.DownloadFragment$1$1$1.run(DownloadFragment.java:73)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:2)

如果您使用ProgressDialog实施AsyncTask,则很容易。 使用以下代码实现进度条。

    private class DownloadTask extends AsyncTask<String, Integer, Void> {

       ProgressDialog dialog = new ProgressDialog(MainActivity.this);

       @Override
       protected void onPreExecute() {
          //set message of the dialog
          dialog.setMessage("Loading...");
          //show dialog
          dialog.show();
          super.onPreExecute();
       }

       protected Void doInBackground(String... args) {
          // do background work here
          return null;
       }

       @Override
       protected void onProgressUpdate(Integer... values) {
           // Update download Progress
           super.onProgressUpdate(values);
       }

       protected void onPostExecute(List<Transaction> result) {
         // do UI work here
         if(dialog != null && dialog.isShowing()){
           dialog.dismiss()
         }
       }
    }