android软件自动升级
package com.henyvision.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.henyvision.flow.R; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; /** * * @author wwj * @date 2012/11/17 * 实现软件更新的管理类 */ public class UpdateManager { //下载中... private static final int DOWNLOAD = 1; //下载完成 private static final int DOWNLOAD_FINISH = 2; //保存解析的XML信息 HashMap<String , String> mHashMap=new HashMap<String , String>(); //下载保存路径 private String mSavePath; //记录进度条数量 private int progress; //是否取消更新 private boolean cancelUpdate = false; //上下文对象 private Context mContext; //进度条 private ProgressBar mProgressBar; //更新进度条的对话框 private Dialog mDownloadDialog; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch(msg.what){ //下载中。。。 case DOWNLOAD: //更新进度条 System.out.println(progress); mProgressBar.setProgress(progress); break; //下载完成 case DOWNLOAD_FINISH: // 安装文件 installApk(); break; } }; }; public UpdateManager(Context context) { super(); this.mContext = context; } /** * 检测软件更新 */ public void checkUpdate() { if (isUpdate()) { //显示提示对话框 showNoticeDialog(); } else { Toast.makeText(mContext, "您现在已经是最新版本了,无需更新", Toast.LENGTH_SHORT).show(); } } private void showNoticeDialog() { // TODO Auto-generated method stub //构造对话框 AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("软件提示"); builder.setMessage("系统检测到新版本"); //更新 builder.setPositiveButton("更新", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); // 显示下载对话框 showDownloadDialog(); } }); // 稍后更新 builder.setNegativeButton("暂不更新", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); Dialog noticeDialog = builder.create(); noticeDialog.show(); } private void showDownloadDialog() { // 构造软件下载对话框 AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("正在下载"); // 给下载对话框增加进度条 final LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.app_update, null); mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar1); builder.setView(view); builder.setNegativeButton("取消安装", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); // 设置取消状态 cancelUpdate = true; } }); mDownloadDialog = builder.create(); mDownloadDialog.show(); //下载文件 downloadApk(); } /** * 下载APK文件 */ private void downloadApk() { // TODO Auto-generated method stub // 启动新线程下载软件 new DownloadApkThread().start(); } /** * 检查软件是否有更新版本 * @return * @throws JSONException */ public boolean isUpdate(){ BufferedReader in = null; // 获取当前软件版本 int versionCode = getVersionCode(mContext); try { URI uri = new URI(ConstantConfig.http + ConstantConfig.ip + ConstantConfig.webName + "repairBill/getVersionMobile.action"); URL url = uri.toURL(); URLConnection connection = url.openConnection(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); connection.connect(); String result =""; //读取URL的响应 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result+=line; } JSONObject jsonObj = new JSONObject(result); Iterator<String> nameItr = jsonObj.keys(); String name; while (nameItr.hasNext()) { name = nameItr.next(); mHashMap.put(name, jsonObj.getString(name)); } } catch (Exception e) { e.printStackTrace(); }finally { try { if (in != null) in.close(); } catch (Exception e2) { e2.printStackTrace(); } } if(null != mHashMap) { int serviceCode = Integer.valueOf(mHashMap.get("version")); //版本判断 if(serviceCode > versionCode) { return true; } } return false; } /** * 获取软件版本号 * @param context * @return */ private int getVersionCode(Context context) { // TODO Auto-generated method stub int versionCode = 0; // 获取软件版本号,对应AndroidManifest.xml下android:versionCode try { versionCode = context.getPackageManager().getPackageInfo( "com.henyvision.flow", 0).versionCode; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return versionCode; } /** * 下载文件线程 * @author Administrator * */ private class DownloadApkThread extends Thread { @Override public void run() { try { //判断SD卡是否存在,并且是否具有读写权限 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 获取SDCard的路径 String sdpath = Environment.getExternalStorageDirectory() + "/"; mSavePath = sdpath + "download"; URL url = new URL(mHashMap.get("url")); // 创建连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); // 获取文件大小 int length = conn.getContentLength(); // 创建输入流 InputStream is = conn.getInputStream(); File file = new File(mSavePath); // 如果文件不存在,新建目录 if (!file.exists()) { file.mkdir(); } File apkFile = new File(mSavePath, mHashMap.get("name")); FileOutputStream fos = new FileOutputStream(apkFile); int count = 0; // 缓存 byte buf[] = new byte[1024]; // 写入到文件中 do { int numread = is.read(buf); count += numread; // 计算进度条的位置 progress = (int) (((float) count / length) * 100); // 更新进度 mHandler.sendEmptyMessage(DOWNLOAD); if (numread <= 0) { // 下载完成 mHandler.sendEmptyMessage(DOWNLOAD_FINISH); break; } // 写入文件 fos.write(buf, 0, numread); } while (!cancelUpdate);//点击取消就停止下载 fos.close(); is.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 取消下载对话框显示 mDownloadDialog.dismiss(); } } /** * 安装APK文件 */ private void installApk() { // String fileName = mSavePath + mHashMap.get("name"); // // Uri uri = Uri.fromFile(new File(fileName)); // // Intent intent = new Intent(Intent.ACTION_VIEW); // intent.setDataAndType(uri, "application/vnd.Android.package-archive"); // // mContext.startActivity(intent); File apkfile = new File(mSavePath, mHashMap.get("name")); if (!apkfile.exists()) { return; } Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); mContext.startActivity(i); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
作者:風巽千龍

浙公网安备 33010602011771号