Android 软件自动更新 (网络XML解析、以及本地xml软件版本两种解析方式)
XML放入网络空间---version.xml .url地址应该为正常***/*****.apk
<update>
<version>5</version>
<name>yuanbao</name>
<url>http://cdn.market.hiapk.com/data/upload//2012/06_27/13/yuanbao.co.
cc_134532.apk
</url>
</update>
----------------主main
package gognzibai.co.cc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//按钮更新
Button updateBtn = (Button) findViewById
(R.id.btnUpdate);
updateBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
UpdateManager manager = new
UpdateManager(MainActivity.this);
manager.checkUpdate();
}
});
}
}
-----------------ParseXmlService.java
package gongzibai.co.cc;
import java.io.InputStream;
import java.util.HashMap;
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;
//解析xml
public class ParseXmlService {
public HashMap<String, String> parseXml(
InputStream inStream)
throws Exception {
HashMap<String, String> hashMap = new HashMap<String,
String>();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory
.newDocumentBuilder();
Document document = builder
.parse(inStream);
Element root = document
.getDocumentElement();
NodeList childNodes = root
.getChildNodes();
for (int j = 0; j < childNodes
.getLength(); j++) {
Node childNode = (Node) childNodes
.item(j);
if (childNode.getNodeType() ==
Node.ELEMENT_NODE) {
Element childElement = (Element)
childNode;
if ("version"
.equals(childElement
.getNodeName())) {
hashMap.put(
"version",
childElement
.getFirstChild()
.getNodeValue());
}
else if (("name"
.equals(childElement
.getNodeName()))) {
hashMap.put(
"name",
childElement
.getFirstChild()
.getNodeValue());
}
else if (("url"
.equals(childElement
.getNodeName()))) {
hashMap.put(
"url",
childElement
.getFirstChild()
.getNodeValue());
}
}
}
return hashMap;
}
}
-------------更新管理.UpdateManager.java
package gongzibai.co.cc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.content.Intent;
import android.content.DialogInte易做图ce.OnClickListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
public class UpdateManager {
private static final int DOWNLOAD = 1;
private static final int DOWNLOAD_FINISH = 2;
HashMap<String, String> mHashMap;
private String mSavePath;
private int progress;
private boolean cancelUpdate = false;
private Context mContext;
private ProgressBar mProgress;
private Dialog mDownloadDialog;
private Handler mHandler = new Handler() {
public void handleMessage(
Message msg) {
switch (msg.what) {
case DOWNLOAD:
mProgress
.setProgress(progress);
break;
case DOWNLOAD_FINISH:
//安装APK
installApk();
break;
default:
break;
}
};
};
public UpdateManager(Context context) {
this.mContext = context;
}
//检查更新
public void checkUpdate() {
if (isUpdate()) {
showNoticeDialog();
} else {
Toast.makeText(
mContext,
R.string.soft_update_no,
Toast.LENGTH_LONG)
.show();
}
}
private boolean isUpdate() {
int versionCode = getVersionCode(mContext);
//本地的XML更新
// InputStream inStream = ParseXmlService.class
//
// .getResourceAsStream("version.xml");
// InputStream inStream = ParseXmlService.class
// .getClassLoader()
// .getResourceAsStream(
// "version1.xml");
//解析网络xml进行更新软件
URL url = null;
try {
补充:移动开发 , Android ,