你的位置:首页 > 操作系统

[操作系统]用xutils3.0进行下载


写的例子比较简单,是用xutils3.0来进行下载项目更新

1.先通过网络请求,判断版本是否要更新

2.若要更新,则弹出一个弹窗,我用的是系统自带的Dialog,将下载的版本号及下载的内容提示展示出来

3.当用户点击下载时,开始下载,下载时展示一个水平的进度条

4.下载完成后,进度条消失,调用系统安装apk

如下是代码  用到的xutils的jar包是3.3.32版本的

 1 package com.item.weixin; 2  3 import java.io.File; 4  5 import org.json.JSONException; 6 import org.json.JSONObject; 7 import org.xutils.x; 8 import org.xutils.common.Callback; 9 import org.xutils.http.RequestParams; 10  11 import android.annotation.SuppressLint; 12 import android.app.Activity; 13 import android.app.AlertDialog; 14 import android.app.ProgressDialog; 15 import android.content.Context; 16 import android.content.DialogInterface; 17 import android.content.Intent; 18 import android.content.pm.PackageInfo; 19 import android.content.pm.PackageManager.NameNotFoundException; 20 import android.net.Uri; 21 import android.os.Bundle; 22 import android.os.Environment; 23 import android.view.View; 24 import android.view.Window; 25 import android.widget.Button; 26  27 /** 28  * 设置xutils下载 29  *  30  * @author Administrator 31  *  32 */ 33 public class LoginActivity extends Activity { 34   private Button ll_update; 35   private ProgressDialog pDialog; 36   private String nowVersion; 37   private ProgressDialog progressDialog; 38  39   @Override 40   protected void onCreate(Bundle savedInstanceState) { 41     // TODO Auto-generated method stub 42     super.onCreate(savedInstanceState); 43     requestWindowFeature(Window.FEATURE_NO_TITLE); 44     setContentView(R.layout.activity_login); 45     ll_update = (Button) findViewById(R.id.btn_login); 46     ll_update.setOnClickListener(new View.OnClickListener() { 47  48       @Override 49       public void onClick(View arg0) { 50         // TODO Auto-generated method stub 51         checkUpdate(); 52       } 53     }); 54     try { 55       PackageInfo packageInfo = getPackageManager().getPackageInfo( 56           getPackageName(), 0); 57       nowVersion = packageInfo.versionName; 58     } catch (NameNotFoundException e) { 59       // TODO Auto-generated catch block 60       e.printStackTrace(); 61     } 62   } 63  64   /** 65    * 下载更新, 66   */ 67   protected void checkUpdate() { 68     // TODO Auto-generated method stub 69     proDialogShow(LoginActivity.this, "正在查询..."); 70     RequestParams params = new RequestParams("url"); 71     x.http().get(params, new Callback.CommonCallback<String>() { 72  73       @Override 74       public void onCancelled(CancelledException arg0) { 75         // TODO Auto-generated method stub 76  77       } 78  79       @Override 80       public void onError(Throwable arg0, boolean arg1) { 81         // TODO Auto-generated method stub 82         PDialogHide(); 83         System.out.println("提示网络错误"); 84       } 85  86       @Override 87       public void onFinished() { 88         // TODO Auto-generated method stub 89  90       } 91  92       @Override 93       public void onSuccess(String arg0) { 94         // TODO Auto-generated method stub 95         PDialogHide(); 96         try { 97           JSONObject object = new JSONObject(arg0); 98           boolean success = object.getBoolean("succee"); 99           if (success) {100             String desc = object.getString("desc");101             String downloadurl = object.getString("downloadurl");102             String versionname = object.getString("versionname");103             if (nowVersion.equals(versionname)) {104               System.out.println("当前版本为最新,不用跟新");105             } else {106               // 不同,弹出更新提示对话框107               setUpDialog(versionname, downloadurl, desc);108             }109           }110         } catch (JSONException e) {111           // TODO Auto-generated catch block112           e.printStackTrace();113         }114       }115     });116   }117 118   /**119    * 120    * @param versionname121    *      地址中版本的名字122    * @param downloadurl123    *      下载包的地址124    * @param desc125    *      版本的描述126   */127   protected void setUpDialog(String versionname, final String downloadurl,128       String desc) {129     // TODO Auto-generated method stub130     AlertDialog dialog = new AlertDialog.Builder(this).setCancelable(false)131         .setTitle("下载" + versionname + "版本").setMessage(desc)132         .setNegativeButton("取消", null)133         .setPositiveButton("下载", new DialogInterface.OnClickListener() {134 135           @Override136           public void onClick(DialogInterface arg0, int arg1) {137             // TODO Auto-generated method stub138             setDownLoad(downloadurl);139           }140         }).create();141     dialog.show();142   }143 144   /**145    * 下载包146    * 147    * @param downloadurl148    *      下载的url149    * 150   */151   @SuppressLint("SdCardPath")152   protected void setDownLoad(String downloadurl) {153     // TODO Auto-generated method stub154     RequestParams params = new RequestParams(downloadurl);155     params.setAutoRename(true);//断点下载156     params.setSaveFilePath("/mnt/sdcard/demo.apk");157     x.http().get(params, new Callback.ProgressCallback<File>() {158 159       @Override160       public void onCancelled(CancelledException arg0) {161         // TODO Auto-generated method stub162         163       }164 165       @Override166       public void onError(Throwable arg0, boolean arg1) {167         // TODO Auto-generated method stub168         if(progressDialog!=null && progressDialog.isShowing()){169           progressDialog.dismiss();170         }171         System.out.println("提示更新失败");172       }173 174       @Override175       public void onFinished() {176         // TODO Auto-generated method stub177         178       }179 180       @Override181       public void onSuccess(File arg0) {182         // TODO Auto-generated method stub183         if(progressDialog!=null && progressDialog.isShowing()){184           progressDialog.dismiss();185         }186         Intent intent = new Intent(Intent.ACTION_VIEW);187         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);188         intent.setDataAndType(Uri.fromFile(new File(Environment189             .getExternalStorageDirectory(), "demo.apk")),190             "application/vnd.android.package-archive");191         startActivity(intent);192       }193 194       @Override195       public void onLoading(long arg0, long arg1, boolean arg2) {196         // TODO Auto-generated method stub197         progressDialog.setMax((int)arg0);198         progressDialog.setProgress((int)arg1);199       }200 201       @Override202       public void onStarted() {203         // TODO Auto-generated method stub204         System.out.println("开始下载");205         progressDialog = new ProgressDialog(LoginActivity.this);206         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为水平进行条207         progressDialog.setMessage("正在下载中...");208         progressDialog.setProgress(0);209         progressDialog.show();210       }211 212       @Override213       public void onWaiting() {214         // TODO Auto-generated method stub215         216       }217     });218   }219 220   private void proDialogShow(Context context, String msg) {221     pDialog = new ProgressDialog(context);222     pDialog.setMessage(msg);223     // pDialog.setCancelable(false);224     pDialog.show();225   }226 227   private void PDialogHide() {228     try {229       if (pDialog != null && pDialog.isShowing()) {230         pDialog.dismiss();231       }232     } catch (Exception e) {233       e.printStackTrace();234     }235   }236 }