應用都有版本的更新,那麼怎樣實現更新呢?今天就為大傢介紹應用自動更新的全過程。
程序自動更新的流程大致如下:
程序啟動 –>適時後臺檢查更新—>鏈接遠程服務器–>獲取新版本信息
–>比對當前版本–>if(有更新)–>顯示更新提示對話框並顯示更新的內容–>交與用戶選擇.
下面是我做的demo,大傢可以參考一下:
佈局比較簡單就不上代碼瞭。
主程序代碼:
[java] package com.cloay.update;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* 程序自動更新
* UpdateTestActivity.java
* @author Cloay
* 2011-11-23
*/
public class UpdateTestActivity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.check);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
checkVersion();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* 檢查是否需要更新程序
* @throws NameNotFoundException
*/
private void checkVersion() throws NameNotFoundException{
UpdateInfo updateInfo = new UpdateInfo();
URL url;
try {
url = new URL("http://localhost:8080/update.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setConnectTimeout(5000);
updateInfo = ParseXmlUtils.parseXml(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(updateInfo.getVersion().equals(VersionUtil.getVersionName(this))){
Toast.makeText(this, "版本相同,不需要升級!", Toast.LENGTH_SHORT).show();
}
else{
showUpdateDialog(updateInfo);
}
}
private void showUpdateDialog(UpdateInfo updateInfo) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("提示檢測到新版本,確定升級嗎?")
.setIcon(R.drawable.ask)
.setMessage(updateInfo.getDescription())
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.show();
}
}
package com.cloay.update;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* 程序自動更新
* UpdateTestActivity.java
* @author Cloay
* 2011-11-23
*/
public class UpdateTestActivity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.check);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
checkVersion();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* 檢查是否需要更新程序
* @throws NameNotFoundException
*/
private void checkVersion() throws NameNotFoundException{
UpdateInfo updateInfo = new UpdateInfo();
URL url;
try {
url = new URL("http://localhost:8080/update.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setConnectTimeout(5000);
updateInfo = ParseXmlUtils.parseXml(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(updateInfo.getVersion().equals(VersionUtil.getVersionName(this))){
Toast.makeText(this, "版本相同,不需要升級!", Toast.LENGTH_SHORT).show();
}
else{
showUpdateDialog(updateInfo);
}
}
private void showUpdateDialog(UpdateInfo updateInfo) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("提示檢測到新版本,確定升級嗎?")
.setIcon(R.drawable.ask)
.setMessage(updateInfo.getDescription())
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.show();
}
}
這裡說明一下:遠程服務器放置一個xml文件,用來說明當前新版本的信息。包括版本號,新版本功能說明,新版下載鏈接
xml解析工具代碼:
[java] <span style="font-size:10px;">package com.cloay.update;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 解析Xml文件
* ParseXmlUtils.java
* @author Cloay
* 2011-11-7
*/
public class ParseXmlUtils {
/**
* 通過InputStream 解析文件
* @param in
* @return
*/
public static UpdateInfo parseXml(InputStream in){
UpdateInfo updateInfo = new UpdateInfo();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
Document doc = null;
doc = db.parse(in);
Element root = doc.getDocumentElement();
NodeList resultNode = root.getElementsByTagName("info");
for(int i = 0; i < resultNode.getLength();i++){
Element res = (Element)resultNode.item(i);
updateInfo.setVersion(res.getElementsByTagName("version").item(0).getFirstChild().getNodeValue());
updateInfo.setUrl(res.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
updateInfo.setDescription(res.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return updateInfo;
}
}
</span>
<span style="font-size:10px;">package com.cloay.update;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 解析Xml文件
* ParseXmlUtils.java
* @author Cloay
* 2011-11-7
*/
public class ParseXmlUtils {
/**
* 通過InputStream 解析文件
* @param in
* @return
*/
public static UpdateInfo parseXml(InputStream in){
UpdateInfo updateInfo = new UpdateInfo();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
Document doc = null;
doc = db.parse(in);
Element root = doc.getDocumentElement();
NodeList resultNode = root.getElementsByTagName("info");
for(int i = 0; i < resultNode.getLength();i++){
Element res = (Element)resultNode.item(i);
updateInfo.setVersion(res.getElementsByTagName("version").item(0).getFirstChild().getNodeValue());
updateInfo.setUrl(res.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
updateInfo.setDescription(res.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return updateInfo;
}
}
</span>
updateInfo實體:
[java] <span style="font-size:10px;">package com.cloay.update;
/**
* 更新信息實體類
* UpdateInfo.java
* @author Cloay
* 2011-11-23
*/
public class UpdateInfo {
private String version; //版本號
private String url; //新版本存放url路徑
private String description; //更新說明信息,比如新增什麼功能特性等
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} </span>
<span style="font-size:10px;">package com.cloay.update;
/**
* 更新信息實體類
* UpdateInfo.java
* @author Cloay
* 2011-11-23
*/
public class UpdateInfo {
private String version; //版本號
private String url; //新版本存放url路徑
private String description; //更新說明信息,比如新增什麼功能特性等
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} </span>
獲取當前已安裝版本信息:
[java] <span style="font-size:10px;">package com.cloay.update;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
* 版本工具類
* VersionUtil.java
* @author Cloay
* 2011-11-23
*/
public class VersionUtil {
/**
* 獲取版本號
* @param context
* 上下文
* @return
* @throws NameNotFoundException
*/
public static String getVersionName(Context context) throws NameNotFoundException{
//獲取PackageManager 實例
PackageManager packageManager = context.getPackageManager();
//獲得context所屬類的包名,0表示獲取版本信息
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
}
}
</span>
<span style="font-size:10px;">package com.cloay.update;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
* 版本工具類
* VersionUtil.java
* @author Cloay
* 2011-11-23
*/
public class VersionUtil {
/**
* 獲取版本號
* @param context
* 上下文
* @return
* @throws NameNotFoundException
*/
public static String getVersionName(Context context) throws NameNotFoundException{
//獲取PackageManager 實例
PackageManager packageManager = context.getPackageManager();
//獲得context所屬類的包名,0表示獲取版本信息
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
}
}
</span>
其實整個過程並不是很麻煩,需要註釋的地方都已詳細註釋瞭,就寫那麼多。有問題請留言大傢一起交流學習!
摘自 Cloay的專欄