Android異步網絡加載庫Ion下載文件
ion是的一個安卓異步網絡和圖片加載庫。
特性
-
異步下載:
</li>-
下載圖片 (用ImageViews 或者 Bitmaps顯示);
</li> -
下載JSON文件 (通過Gson解析);
</li> -
下載字符串;
</li> -
下載文件;
</li> </ul> -
Fluent API;
</li> -
當呼叫活動(calling Activity)結束時,可以自動取消網絡操作。
</li> -
所有的操作都返回一個Future(http://en.wikipedia.org/wiki/Futures_and_promises),并且可以取消;
</li> -
HTTP POST/PUT;
</li> -
緩存;
</li> -
Gzip/Deflate 壓縮
</li> -
當有多個IP地址時,自動選擇最好最穩定的服務器連接; 9. 支持Cookies 等等。
</li> </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/(10241024)), (int)downloaded/(10241024), false); builder.setContentText(downloaded/(10241024)+ "."+downloaded%(10241024)+ "M"+ " / " + (total/(10241024))+"."+(total%(10241024))+"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); } } }); }</pre><br />
根據請求,使用Notification的形式動態展示 當前下載進度展示,并設定了 PendingIntent ,點擊Notification時,跳轉特定頁面,并展示當前下載進度。
-
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!