【开源】Groundy

Groundy

  •  https://github.com/casidiablo/groundy

    介绍:

    异步下载文件库,使用service开启线程下载并在界面中使用progressbar显示下载进度。其实GB之后DownloadManager已经很完善的实现了下载功能,但我觉得掌握类似于Groundy 这样的库还是有它的用武之处的。

    运行效果:

    使用说明:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public class MainActivity extends Activity {
        private ProgressDialog mProgressDialog;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            findViewById(R.id.btn_download).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    String url = ((EditText) findViewById(R.id.edit_url)).getText().toString().trim();
                    Bundle extras = new Bundler().add(DownloadTask.PARAM_URL, url).build();
                    Groundy.create(DownloadExample.this, DownloadTask.class)
                            .receiver(mReceiver)
                            .params(extras)
                            .queue();
                    mProgressDialog = new ProgressDialog(MainActivity.this);
                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    mProgressDialog.setCancelable(false);
                    mProgressDialog.show();
                }
            });
        }
        private ResultReceiver mReceiver = new ResultReceiver(new Handler()) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);
                switch (resultCode) {
                    case Groundy.STATUS_PROGRESS:
                        mProgressDialog.setProgress(resultData.getInt(Groundy.KEY_PROGRESS));
                        break;
                    case Groundy.STATUS_FINISHED:
                        Toast.makeText(DownloadExample.this, R.string.file_downloaded, Toast.LENGTH_LONG);
                        mProgressDialog.dismiss();
                        break;
                    case Groundy.STATUS_ERROR:
                        Toast.makeText(DownloadExample.this, resultData.getString(Groundy.KEY_ERROR), Toast.LENGTH_LONG).show();
                        mProgressDialog.dismiss();
                        break;
                }
            }
        };
    }

    其中GroundyTask的定义如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class DownloadTask extends GroundyTask {
        public static final String PARAM_URL = "com.groundy.sample.param.url";
        @Override
        protected boolean doInBackground() {
            try {
                String url = getParameters().getString(PARAM_URL);
                File dest = new File(getContext().getFilesDir(), new File(url).getName());
                DownloadUtils.downloadFile(getContext(), url, dest, DownloadUtils.getDownloadListenerForTask(this));
                return true;
            } catch (Exception pokemon) {
                return false;
            }
        }
    }

posted on 2015-03-31 12:22  wasdchenhao  阅读(159)  评论(0)    收藏  举报

导航