Android updataApp更新安装方法封装,
前段时间一直忙着找工作的事,就没有什么时间去更新博客了。现在已经到新公司上班了。没事就先更新下吧,这段时间觉得自己还是有好多东西需要学习,每一次面试都感觉自己可以知道很多东西,比如现在主流什么,大家都用什么的东西等,都还挺不错的,不过比较不喜欢换工作了。比较烦躁。 好了抱怨就到这里了。开始今天的正题吧,
上代码:
/* @author x.j 更新安裝APK * @time 2014.09.18 */ public class UploadApp { private ProgressDialog mpDialog;// 创建精度条 private int fileSize;// 设置文件大小 private int downLoadFileSize;// 当前已下载的文件的大小 private Context mContext; // APK的安装路径 private static final String savePath = "/sdcard/updatedemo/"; //保存下载文件的路径 private static final String saveFileName = savePath + "UpdateDemo.apk";//保存下载文件的名称 /** * 提示用户更新 * * @param mcontext * @param url.下载链接 * @param str.更新内容 */ public void uploadApp(Context mcontext, String str, final String url) { this.mContext = mcontext; AlertDialog.Builder builder = new Builder(mContext); builder.setMessage("有新的版本升级,是否下载安装?\n" + str); builder.setTitle("系统版本更新");// str可以提示的内容显示 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mpDialog = new ProgressDialog(mContext); mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mpDialog.setTitle("提示"); mpDialog.setMessage("正在下载中,请稍后"); mpDialog.setIndeterminate(false);// 是进度条是否明确 mpDialog.setCancelable(false);// 点击返回按钮的时候无法取消对话框 mpDialog.setCanceledOnTouchOutside(false);// 点击对话框外部取消对话框显示 mpDialog.setProgress(0);// 设置初始进度条为0 mpDialog.incrementProgressBy(1);// 设置进度条增涨。 mpDialog.show(); new Thread() { public void run() { String apkUrl = url;// 下载APK的url URL url = null; try { url = new URL(apkUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); InputStream in = con.getInputStream(); fileSize = con.getContentLength();// 获取下载文件的长度 File file = new File(savePath); if (!file.exists()) { file.mkdir(); File fileOut = new File(saveFileName);// 下载文件的存放地址 FileOutputStream out = new FileOutputStream(fileOut); byte[] bytes = new byte[1024]; downLoadFileSize = 0; sendMsg(0);// sendMeg为0的时候显示下载完成 int c; while ((c = in.read(bytes)) != -1) { out.write(bytes, 0, c); downLoadFileSize += c; sendMsg(1); } in.close(); out.close(); } } catch (Exception e) { e.printStackTrace(); } sendMsg(2); } }.start(); dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } // 安装apk方法 private void installApk(String filename) { File file = new File(filename); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive"; intent.setDataAndType(Uri.fromFile(file), type); mContext.startActivity(intent); if (mpDialog != null) { mpDialog.cancel(); } } private void sendMsg(int flag) { Message msg = new Message(); msg.what = flag; handler.sendMessage(msg); } private final Handler handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg) { if (!Thread.currentThread().isInterrupted()) { switch (msg.what) { case 0: mpDialog.setMax(100); break; case 1: int result = downLoadFileSize * 100 / fileSize; mpDialog.setProgress(result); break; case 2: mpDialog.setMessage("文件下载完成"); installApk(saveFileName); break; case -1: String error = msg.getData().getString("error"); mpDialog.setMessage(error); break; default: break; } } super.handleMessage(msg); } }; }
<!--往sdcard中写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <!--在sdcard中创建/删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。