android app版本更新简介

一、访问服务器,获取最新版的版本信息

/** 获取服务端版本号 */
	public static int getVersion(String url) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);

		HttpResponse response;
		try {
			response = client.execute(get);
			//连接成功
			if (200 == response.getStatusLine().getStatusCode()) {
				InputStream in = response.getEntity().getContent();
				//将版本文件下载到指定的位置
				File file = new File(Environment.getExternalStorageDirectory()
						.toString() + "/version.xml");
				FileOutputStream fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len;
				while ((len = in.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
				}
				fos.flush();
				in.close();
				fos.close();
				FileInputStream fis = new FileInputStream(file);
				//对版本文件进行XML解析
				int code = parseXml(fis);
				return code;
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
XML解析的方法可参考如下,根据不同的xml格式需部分修改本方法:

private static int parseXml(InputStream is) {
		int code = 0;
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(is, "utf-8");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					String tag = parser.getName();
					if ("VERSION".equals(tag)) {
						String codeStr = parser.nextText().trim();
						code = Integer.parseInt(codeStr.substring(0, 1));
					}
					if ("SIZE".equals(tag)) {
						String len = parser.nextText().trim();
						leng = Long.parseLong(len);
					}
					break;
				default:
					break;
				}
				eventType = parser.next();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

二、根据获取到的最新版本号,与当前使用版本进行比较,如需升级则进行如下操作。

从服务器端获取最新安装包,方法如下:

/** 获取最新安装包 */
	public static void getApk(String url) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		HttpResponse response;
		try {
			response = client.execute(get);
			if (200 == response.getStatusLine().getStatusCode()) {
				InputStream in = response.getEntity().getContent();
				//将安装包下载到本地
				File file = new File(Environment.getExternalStorageDirectory()
						.toString() + "/xxx/version/xxx.apk");
				FileOutputStream fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len;
				while ((len = in.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
					int rate = (int) (file.length() * 100 / leng);
					//此处可进行UI更新
				}
				fos.flush();
				in.close();
				fos.close();
				return;
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return;
	}
三、下载完安装包可直接进行安装,安装方法如下:

// 安装apk
	protected void installApk(File file) {
		Intent intent = new Intent();
		// 执行动作
		intent.setAction(Intent.ACTION_VIEW);
		// 执行的数据类型
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		startActivity(intent);
	}




猜你喜欢

转载自blog.csdn.net/huashanjuji/article/details/49001071