【开源】Groundy
Groundy
- https://github.com/casidiablo/groundy
介绍:
异步下载文件库,使用service开启线程下载并在界面中使用progressbar显示下载进度。其实GB之后DownloadManager已经很完善的实现了下载功能,但我觉得掌握类似于Groundy 这样的库还是有它的用武之处的。运行效果:
![]()
使用说明:
1234567891011121314151617181920212223242526272829303132333435363738394041public class MainActivity extends Activity {private ProgressDialog mProgressDialog;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.btn_download).setOnClickListener(newView.OnClickListener() {public void onClick(View view) {String url = ((EditText) findViewById(R.id.edit_url)).getText().toString().trim();Bundle extras =newBundler().add(DownloadTask.PARAM_URL, url).build();Groundy.create(DownloadExample.this, DownloadTask.class).receiver(mReceiver).params(extras).queue();mProgressDialog =newProgressDialog(MainActivity.this);mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);mProgressDialog.setCancelable(false);mProgressDialog.show();}});}private ResultReceiver mReceiver =newResultReceiver(newHandler()) {@Overrideprotected void onReceiveResult(int resultCode, Bundle resultData) {super.onReceiveResult(resultCode, resultData);switch(resultCode) {caseGroundy.STATUS_PROGRESS:mProgressDialog.setProgress(resultData.getInt(Groundy.KEY_PROGRESS));break;caseGroundy.STATUS_FINISHED:Toast.makeText(DownloadExample.this, R.string.file_downloaded, Toast.LENGTH_LONG);mProgressDialog.dismiss();break;caseGroundy.STATUS_ERROR:Toast.makeText(DownloadExample.this, resultData.getString(Groundy.KEY_ERROR), Toast.LENGTH_LONG).show();mProgressDialog.dismiss();break;}}};}其中
GroundyTask的定义如下:1234567891011121314public class DownloadTask extends GroundyTask {public static final String PARAM_URL ="com.groundy.sample.param.url";@Overrideprotected boolean doInBackground() {try{String url = getParameters().getString(PARAM_URL);File dest =newFile(getContext().getFilesDir(),newFile(url).getName());DownloadUtils.downloadFile(getContext(), url, dest, DownloadUtils.getDownloadListenerForTask(this));returntrue;}catch(Exception pokemon) {returnfalse;}}}
posted on 2015-03-31 12:22 wasdchenhao 阅读(159) 评论(0) 收藏 举报

浙公网安备 33010602011771号