标签:
1.获取当前包的信息:
1 PackageManager manager = Main.this.getPackageManager();
2 try {
3 PackageInfo info = manager.getPackageInfo(Main.this.getPackageName(), 0);
4 String appVersion = info.versionName; // 版本名
5 currentVersionCode = info.versionCode; // 版本号
6 System.out.println(currentVersionCode + " " + appVersion);
7 } catch (NameNotFoundException e) {
8 // TODO Auto-generated catch blockd
9 e.printStackTrace();
10 }
11 //上面是获取manifest中的版本数据,使用了versionCode
12 //在从服务器获取到最新版本的versionCode,比较
13 showUpdateDialog();
2.直接启动线程进行下载管理并安装:
1 private void showUpdateDialog() { 2 AlertDialog.Builder builder = new AlertDialog.Builder(this); 3 builder.setTitle("检测到新版本"); 4 builder.setMessage("是否下载更新?"); 5 builder.setPositiveButton("更新", new DialogInterface.OnClickListener() { 6 7 @Override 8 public void onClick(DialogInterface dialog, int which) { 9 // TODO Auto-generated method stub11 showDownloadDialog(); dialog.dismiss(); 14 } 15 }).setNegativeButton("稍后更新", new DialogInterface.OnClickListener() { 16 17 @Override 18 public void onClick(DialogInterface dialog, int which) { 19 // TODO Auto-generated method stub 20 dialog.dismiss(); 21 } 22 }); 23 builder.show(); 24 }
3.弹出下载更新进度框,进行apk下载更新:
1 private void showDownloadDialog(){ 2 AlertDialog.Builder builder = new Builder(mContext); 3 builder.setTitle("软件版本更新"); 4 5 final LayoutInflater inflater = LayoutInflater.from(mContext); 6 View v = inflater.inflate(R.layout.progress, null); 7 mProgress = (ProgressBar)v.findViewById(R.id.progress); 8 9 builder.setView(v); 10 builder.setNegativeButton("取消", new OnClickListener() { 11 @Override 12 public void onClick(DialogInterface dialog, int which) { 13 dialog.dismiss(); 14 interceptFlag = true; 15 } 16 }); 17 downloadDialog = builder.create(); 18 downloadDialog.show(); 19 20 downloadApk(); 21 }
4.启动线程进行下载任务:
1 /** 2 * 下载apk 3 */ 4 5 private void downloadApk(){ 6 downLoadThread = new Thread(mdownApkRunnable); 7 downLoadThread.start(); 8 }
5.对于下载更新进度框的控制:interceptFlag
1 private Runnable mdownApkRunnable = new Runnable() { 2 @Override 3 public void run() { 4 try { 5 URL url = new URL(apkUrl); 6 7 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 8 conn.connect(); 9 int length = conn.getContentLength(); 10 InputStream is = conn.getInputStream(); 11 12 File file = new File(savePath); 13 if(!file.exists()){ 14 file.mkdir(); 15 } 16 String apkFile = saveFileName; 17 File ApkFile = new File(apkFile); 18 FileOutputStream fos = new FileOutputStream(ApkFile); 19 20 int count = 0; 21 byte buf[] = new byte[1024]; 22 23 do{ 24 int numread = is.read(buf); 25 count += numread; 26 progress =(int)(((float)count / length) * 100); 27 //更新进度 28 mHandler.sendEmptyMessage(DOWN_UPDATE); 29 if(numread <= 0){ 30 //下载完成通知安装 31 mHandler.sendEmptyMessage(DOWN_OVER); 32 break; 33 } 34 fos.write(buf,0,numread); 35 }while(!interceptFlag);//点击取消就停止下载. 36 37 fos.close(); 38 is.close(); 39 } catch (MalformedURLException e) { 40 e.printStackTrace(); 41 } catch(IOException e){ 42 e.printStackTrace(); 43 } 44 45 } 46 };
6.进行提示框进度条更新:
1 private Handler mHandler = new Handler(){ 2 public void handleMessage(Message msg) { 3 switch (msg.what) { 4 case DOWN_UPDATE: 5 mProgress.setProgress(progress); 6 break; 7 case DOWN_OVER: 8 9 installApk(); 10 break; 11 default: 12 break; 13 } 14 }; 15 };
7.下载完成,进行apk安装:
1 /** 2 * 安装apk 3 */ 4 private void installApk(){ 5 File apkfile = new File(saveFileName); 6 if (!apkfile.exists()) { 7 return; 8 } 9 Intent i = new Intent(Intent.ACTION_VIEW); 10 i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); 11 mContext.startActivity(i); 12 13 }
标签:
原文地址:http://www.cnblogs.com/CharlesGrant/p/4773429.html