General process: the Android client accesses the xml file on the server that encapsulates version numbers and other information, and compares the version on the server with the current version,
If the version is lower than the version of the server, download the new version of software on the server, install and replace it, and complete the upgrade.
1. First, use tomcat to build a server for development and testing.
Download tomcat refer to: http://blog.csdn.net/only_tan/article/details/25110625
1. Create your own project in tomcat:
Create a new project folder under the \ apache-tomcat-6.0.39 \ webapps directory, such as myapp
Then, Put xml, apk, and other files in the myapp folder;
For example, the upgrade.xmland test.apk files are stored in my myappproject;
2. Access your project:
Open xml: http: // localhost: 8080/myapp/upgrade. xml in the PC browser
Open xml: http: // 10.0.2.2: 8080/myapp/upgrade. xml on the Android device (or replace 10.0.2.2 with the local IP address)
The method for opening the apk file is the same. Normally, the download dialog box is displayed.
For example, http: // 10.0.2.2: 8080/myapp/Test.apk of Test.apk
If the file in tomcat cannot be downloaded (such as apk), the solution is as follows:
Method 1:
Put the file to be downloaded in the tomcat/webapps/ROOT directory, and access http: // local IP Address: 8080/downloaded file name (or http: // 10.0.2.2: 8080/file name );
Method 2: Add the following content to apache-tomcat-6.0.39 \ conf \ web. xml:
doc
application/msword
xls
application/msexcel
pdf
application/pdf
zip
application/zip
rar
application/rar
txt
application/txt
chm
application/mshelp
Save and restart tomcat.
2. access the server and parse the xml file content;
1. xml file content on the server: (it mainly contains three elements: version number, url address, and related information)
2
Http: // 10.0.2.2: 8080/myapp/Test.apk
Version updated to 2.0. Thank you!
2. Create an object class: (encapsulate the required version and other elements for xml content)
public class UpdataInfo { private int version; private String url; private String about; public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getAbout() { return about; } public void setAbout(String about) { this.about = about; } }
3. Use the pull parser to parse xml content. (An InputStream is required)
Public UpdataInfo getUpdataInfo (InputStream is) throws Exception {UpdataInfo info = null; XmlPullParser parser = Xml. newPullParser (); parser. setInput (is, "GB2312"); // set the parsed data source. The encoding format is int event = parser. getEventType (); while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_DOCUMENT: // start parsing // initialization can be performed here. info = new UpdataInfo (); Log. I ("UpdatePullParser", "-- START_DOCUMENT --"); break; case XmlPullParser. START_TAG: Log. I ("UpdatePullParser", "-- START_TAG --"); String tag = parser. getName (); if ("version ". equals (tag) {info. setVersion (new Integer (parser. nextText (); // get the version number} else if ("url ". equals (tag) {info. setUrl (parser. nextText (); // obtain the url} else if ("about ". equals (tag) {info. setAbout (parser. nextText (); // obtain related information} break; case XmlPullParser. END_TAG: // read an element. If multiple elements exist, store them in the container for break; default: break;} event = parser. next () ;}return info; // returns an UpdataInfo object}
4. Get the xml content and return an InputStream for parsing;
Public InputStream getXml () throws Exception {String TAG = "URLConnect"; String httpUrl = "http: // 10.0.2.2: 8080/myapp/upgrade. xml "; HttpURLConnection conn = (HttpURLConnection) new URL (httpUrl ). openConnection (); conn. setReadTimeout (5*1000); // set the connection timeout time // conn. setRequestMethod ("GET"); conn. connect (); // start to connect if (conn. getResponseCode () == 200) {InputStream is = conn. getInputStream (); return is; // return InputStream} else {Log. e (TAG, "--- Connection Failed ---");} conn. disconnect (); // disconnect return null ;}
5. Call a trigger event to obtain the server version number;
Private int mLocalVersion = 1; // local private int mServerVersion = 2; // server version // call the method for obtaining and parsing xml (asynchronous or in-thread operations ); private void check () {new Thread () {@ Override public void run () {// method to be executed in the Thread try {InputStream is = getXml (); // obtain the xml content UpdataInfo info = getUpdataInfo (is); // call the parsing method mServerVersion = info. getVersion (); // obtain the server version Log. I (TAG, "check -- infoVersion =" + info. getVersion () + "infoURL =" + info. getUrl () + "infoAbo Ut = "+ info. getAbout ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} // send a message to handler. sendEmptyMessage (new Message (). what = 1 );}}. start () ;}// Handler Message receiving mechanism private Handler mHandler = new Handler () {// Handler receives the corresponding Message and refresh the ui and other operations public void handleMessage (Message msg) {super. handleMessage (msg); if (msg. what = 1) {// receives the message and performs ui operations here, such as displaying the parsed content. }}};
6. Obtain the local version number and compare it with the server version number;
// Obtain the local version method void getLocal () {PackageInfo packageInfo; try {packageInfo = getApplicationContext (). getPackageManager (). getPackageInfo (getPackageName (), 0); mLocalVersion = packageInfo. versionCode;} catch (NameNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();}}By now, both the server version and local version are available, and the rest is the comparison size;
If (mLocalVersion <mServerVersion) {// There is a new version, you need to upgrade} else {// No upgrade required}
---------------------------- OK, record it here first. If you are free, continue ----------------- thanks! -------------------