弹框--更新下载--安装
2023-09-14 09:00:22 时间
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 * 安装apk 37 */ 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 * 下载APK 49 */ 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 block 87 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 path 15 * 服务器文件路径 16 * @param filepath 17 * 本地文件路径 18 * @return 本地文件对象 19 * @throws Exception 20 */ 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 }
相关文章
- 下载whl文件,离线方式安装numpy包_python离线安装pip
- IntelliJ IDEA 2019软件下载和安装教程
- Lr软件下载Lightroom Classic各版本安装技巧--LRC介绍
- Photoshop CS4软件下载地址及安装教程(mac+windows全版本PS,包括最新2023)
- Auto CAD2018软件下载地址及安装教程--cad所有版本!
- AutoCAD2009软件下载地址及安装教程--cad所有版本!
- Audition 2018 For Mac软件安装教程 AU软件全版本下载
- AutoCAD2008软件下载地址及安装教程--cad所有版本!
- Auto CAD2019软件下载地址及安装教程--cad所有版本!
- AutoCAD2005软件下载地址及安装教程--cad所有版本!
- CAD2015软件下载及安装教程--cad所有版本!
- CAD2006软件下载与安装--cad所有版本!
- CAD2010软件下载及安装教程--cad所有版本!
- Photoshop CC2020电脑软件下载 简单软件安装教程-PS软件全版本
- PS 2018软件安装下载完美中文版小白教程--PS软件全版本
- PS cs6软件下载及安装--PS软件全版本
- Pr CS4 软件安装教程--Premiere全版本软件下载
- CAD2021免费下载及安装教程--autocad软件所有版本
- Adobe Bridge2023安装下载使用教程
- 排版软件Id下载:InDesign 2023中文版本安装激活教程 --干货分享
- office2016特殊版安装下载教程--全版本office办公软件下载
- PTC Creo5.0下载地址及安装教程--多个版本Creo-CAD软件
- HyperMesh2021 安装教程及软件下载--多版本HyperWorks安装包
- AN软件安装包 adobe animate --技巧分享--安装下载
- Vegas Pro电脑版下载,Vegas Pro(视频处理)官方最新版安装教程
- 核磁共振数据处理MestReNova 软件安装包下载及安装教程
- 安装Oracle实例安装指南:一步步搭建你的系统(oracle实例下载)
- MySQL5.7压缩包下载及安装教程(mysql5.7压缩包)
- Oracle优化器快速安装实现良好性能(oracle优化器下载)