Android异步网络加载库Ion下载文件
jopen
9年前
ion是的一个安卓异步网络和图片加载库。
特性
-
异步下载:
-
下载图片 (用ImageViews 或者 Bitmaps显示);
-
下载JSON文件 (通过Gson解析);
-
下载字符串;
-
下载文件;
-
Fluent API;
-
当呼叫活动(calling Activity)结束时,可以自动取消网络操作。
-
所有的操作都返回一个Future(http://en.wikipedia.org/wiki/Futures_and_promises),并且可以取消;
-
HTTP POST/PUT;
-
缓存;
-
Gzip/Deflate 压缩
-
当有多个IP地址时,自动选择最好最稳定的服务器连接; 9. 支持Cookies 等等。
</ul>
Github 项目地址为: ion github
Eclipse jar包导入形式:
需要使用到两个jar 分别为: androidasync.jar 和 Ion.jar
个人测试 下载文件部分代码如下:
public static void NewDownload(RequestParams params,final Context mcontext,final BaseInfo t){ progressBar = new ProgressBar(mcontext); progressDialog = new ProgressDialog(mcontext); LogCat.i(Constants.getLogTag(), Constants.getRequestHost()+t.getDownloadUrl()); final NotificationManager nm = (NotificationManager) mcontext.getSystemService(Context.NOTIFICATION_SERVICE); final Notification.Builder builder = new Notification.Builder(mcontext); builder.setWhen(System.currentTimeMillis()).setContentTitle(t.getName()+ "下载中").setContentText(t.getVersionName()); builder.setLargeIcon(bitmap); builder.setSmallIcon(R.drawable.download_icon); File file = Environment.getExternalStorageDirectory(); File file2 = new File(file, "down"); file2.mkdir(); file2 = new File(file2,t.getName()+ ".apk"); Ion.with(mcontext).load(Constants.getRequestHost()+t.getDownloadUrl()).progressBar(progressBar).progressDialog(progressDialog) .progress( new ProgressCallback() { @Override public void onProgress( long downloaded, long total) { builder.setProgress(( int)(total/(1024*1024)), (int)downloaded/(1024*1024), false); builder.setContentText(downloaded/(1024*1024)+ "."+downloaded%(1024*1024)+ "M"+ " / " + (total/(1024*1024))+"."+(total%(1024*1024))+"M" ); Intent intent = new Intent(mcontext,DownloadingActivity.class ); Bundle bundle = new Bundle(); bundle.putLong( "downloaded", downloaded); bundle.putLong( "total", total); bundle.putSerializable( "appInfo", t); intent.putExtras(bundle); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendintent= PendingIntent.getActivity(mcontext, Notification_id, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendintent); Notification notification = builder.getNotification(); nm.notify(Notification_id, notification); } }).write(file2).setCallback( new FutureCallback<File>() { @Override public void onCompleted(Exception e, File file) { if (e == null) { AppUtil.install(mcontext, Environment.getExternalStorageDirectory()+File.separator+"down" +File.separator+t.getName()+".apk"); nm.cancel(Notification_id); } else{ Utils.showToast(mcontext, "下载失败,请稍后重试" ); nm.cancel(Notification_id); } } }); }
根据请求,使用Notification的形式动态展示 当前下载进度展示,并设定了 PendingIntent ,点击Notification时,跳转特定页面,并展示当前下载进度。