首页 > 代码库 > 弹框--更新下载--安装

弹框--更新下载--安装

 1     /** 2      * show出对话框 3      */ 4     private void showUpdataDialog() { 5         AlertDialog.Builder builder = new Builder(this); 6         builder.setTitle("版本升级"); 7         builder.setMessage(info.getDescription()); 8         builder.setCancelable(false); 9         builder.setPositiveButton("是", new OnClickListener() {10             public void onClick(DialogInterface dialog, int which) {11                 System.out.println("升级");12                 // 判断SD卡状态是否可用13                 if (Environment.getExternalStorageState().equals(14                         Environment.MEDIA_MOUNTED)) {15                     // 下载是耗时的工作,需要放入到子线程里面去执行16                     DownLoadFileThreadTask download = new DownLoadFileThreadTask(17                             info.getApkurl(), "/sdcard/new.apk");18                     pd.show();19                     new Thread(download).start();20                     mainUI();21                 } else {22 23                 }24             }25         });26         builder.setNegativeButton("否", new OnClickListener() {27             public void onClick(DialogInterface dialog, int which) {28                 System.out.println("不升级,进入主界面");29                 mainUI();30             }31         });32         builder.create().show();33     }34 35     /**36      * 安装apk37      */38     private void installAPK(File file) {39         Intent intent = new Intent();40         intent.setAction(Intent.ACTION_VIEW);41         intent.setDataAndType(Uri.fromFile(file),42                 "application/vnd.android.package-archive");43         finish();44         startActivity(intent);45     }46 47     /**48      * 下载APK49      */50     private class DownLoadFileThreadTask implements Runnable {51         String apkurl;52         String fileurl;53 54         public DownLoadFileThreadTask(String apkurl, String fileurl) {55             this.apkurl = apkurl;56             this.fileurl = fileurl;57         }58 59         public void run() {60             DownLoadFileTask down = new DownLoadFileTask();61             try {62                 File file = down.getFile(apkurl, fileurl, pd);63                 System.out.println("下载完成,进行安装");64                 pd.dismiss();65                 installAPK(file);66             } catch (Exception e) {67                 e.printStackTrace();68                 Toast.makeText(getApplicationContext(), "下载文件失败", 0).show();69                 pd.dismiss();70                 mainUI();71             }72         }73     }74 75     /**76      * 获得版本号77      */78     private String getVession() {79 80         try {81             PackageManager pm = getPackageManager();82             // 第一个参数获得包的名字83             PackageInfo info = pm.getPackageInfo(getPackageName(), 0);84             return info.versionName;85         } catch (Exception e) {86             // TODO Auto-generated catch block87             e.printStackTrace();88         }89         return null;90     }

 

 1 package cn.itcast.mobilesafe.engine; 2  3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import android.app.ProgressDialog; 9 10 public class DownLoadFileTask {11 12     /**13      * 14      * @param path15      *            服务器文件路径16      * @param filepath17      *            本地文件路径18      * @return 本地文件对象19      * @throws Exception20      */21     public static File getFile(String path, String filepath, ProgressDialog pd)22             throws Exception {23         URL url = new URL(path);24         HttpURLConnection conn = (HttpURLConnection) url.openConnection();25         conn.setRequestMethod("GET");26         conn.setConnectTimeout(5000);27         if (conn.getResponseCode() == 200) {28             int total = conn.getContentLength();29             pd.setMax(total);30             InputStream is = conn.getInputStream();31             File file = new File(filepath);32             FileOutputStream fos = new FileOutputStream(file);33             byte[] buffer = new byte[1024];34             int len = 0;35             int process = 0;36             while ((len = is.read(buffer)) != -1) {37                 fos.write(buffer, 0, len);38                 process += len;39                 pd.setProgress(process);40                 Thread.sleep(50);41             }42             fos.flush();43             fos.close();44             is.close();45 46             return file;47         }48         return null;49     }50 }

 

弹框--更新下载--安装