Android应用自动更新及安装

http://blog.csdn.net/encienqi/article/details/8291810

 

由于Android项目开源所致,有很多安卓软件市场。为了让我们开发的软件有更多的用户使用,我们需要向很多市场发布,软件升级后,我们也必须到安卓市场上进行更新,给我们增加了工作量。因此我们有必要给我们的Android应用增加自动更新的功能。那么实现自动更新,我们首先必须让我们的应用知道是否存在新版本的软件,因此我们可以在自己的网站上放置配置文件,存放软件的版本信息:

下面就续上本次知识点的相关内容:

(1)需要解析的xml文件:update.xml文件,此文件放置在服务器上:

<?xml version="1.0" encoding="utf-8"?>
<updates>
<update id="2">  
    <version>2</version>
	<name>应用程序安装</name>
	<url>http://10.0.1.163/shine/one.apk</url>
</update>  
</updates>


(2)在这里我们使用xml文件进行信息的读取,且由于xml文件比较小,故而此处可以通过DOM方式进行xml文件的解析

 

xml文件读取类:ParseXmlService.java

package com.shine.update;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.AsyncTask;

public class ParseXmlService {

	public List<HashMap<String, String>> getContactAll() throws Exception {
		List<HashMap<String, String>> contacts = null;
		String path = "http://10.0.1.163/shine/update.xml";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(3000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			InputStream is = conn.getInputStream();
			// 这里获取数据直接放在xmlpullparser里面解析;
			contacts = parseXml(is);
			System.out.println(contacts.get(0).get("name")
					+ "======================================");
			System.out.println(contacts.get(0).get("version")
					+ "======================================");
			System.out.println(contacts.get(0).get("url")
					+ "======================================");
			return contacts;
		}
		return null;
	}

	public List<HashMap<String, String>> parseXml(InputStream inStream)
			throws Exception {
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> hashMap = null;
		// 实例化一个文档构建器工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 通过文档构建器工厂获取一个文档构建器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 通过文档通过文档构建器构建一个文档实例
		Document document = builder.parse(inStream);
		// 获取XML文件根节点
		Element root = document.getDocumentElement();
		// 获得所有子节点
		NodeList updates = root.getChildNodes();
		if (updates != null) {
			hashMap = new HashMap<String, String>();
			for (int i = 0; i < updates.getLength(); i++) {
				Node update = updates.item(i);
				if (update.getNodeType() == Node.ELEMENT_NODE) {
					String email = update.getAttributes().getNamedItem("id")
							.getNodeValue();
					System.out.println("+++++++id++++++" + email);
					hashMap.put("id", update.getAttributes().getNamedItem("id")
							.getNodeValue());

					for (Node node = update.getFirstChild(); node != null; node = node
							.getNextSibling()) {
						if (node.getNodeType() == Node.ELEMENT_NODE) {
							if (node.getNodeName().equals("version&
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值