1353201425_8069  1353201447_2896

   

    

具體步驟:

1. 在服務器上部署更新所用的xml文件:version.xml

<update>
	<version>2</version>
	<name>baiduxinwen.apk</name>
	<url>http://gdown.baidu.com/data/wisegame/e5f5c3b8e59401c8/baiduxinwen.apk</url>
</update>

 

 

 

 

2. 在客戶端實現更新操作

  涉及到三個技術:

   1.xml文件的解析

   2.HttpURLConnection連接

   3.文件流I/O

 

這裏創建一個解析xml文件的服務類:ParXmlService.java

package com.xiaowu.news.update;

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;

public class ParseXmlService {
	public HashMap<String, String> parseXml (InputStream inStream) throws Exception{
		HashMap<String, String> hashMap = new HashMap<String, String>();
		//創建DocumentBuilderFactory,該對象將創建DocumentBuilder。
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//創建DocumentBuilder,DocumentBuilder將實際進行解析以創建Document對象
		DocumentBuilder builder = factory.newDocumentBuilder();
		//解析該文件以創建Document對象
		Document document = builder.parse(inStream);
		//獲取XML文件根節點 
		Element root = document.getDocumentElement();
		//獲得所有子節點
		NodeList childNodes = root.getChildNodes();
		for(int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = (Node) childNodes.item(i);
			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 com.xiaowu.news.update;

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 javax.net.ssl.HttpsURLConnection;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.xiaowu.news.R;

/**
 * 
 * @author wwj
 * @date 2012/11/17
 * 實現軟件更新的管理類
 */
public class UpdateManager {
	
	//下載中...
	private static final int DOWNLOAD = 1;
	//下載完成
	private static final int DOWNLOAD_FINISH = 2;
	//保存解析的XML信息
	HashMap<String , String> mHashMap;
	//下載保存路徑
	private String mSavePath;
	//記錄進度條數量
	private int progress;
	//是否取消更新
	private boolean cancelUpdate = false;
	//上下文對象
	private Context mContext;
	//進度條
	private ProgressBar mProgressBar;
	//更新進度條的對話框
	private Dialog mDownloadDialog;
	
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			//下載中。。。
			case DOWNLOAD:
				//更新進度條
				System.out.println(progress);
				mProgressBar.setProgress(progress);
				break;
			//下載完成
			case DOWNLOAD_FINISH:
				// 安裝文件
				installApk();
				break;
			}
		};
	};


	public UpdateManager(Context context) {
		super();
		this.mContext = context;
	}
	
	
	/**
	 * 檢測軟件更新
	 */
	public void checkUpdate() {
		if (isUpdate()) {
			//顯示提示對話框
			showNoticeDialog();
		} else {
			Toast.makeText(mContext, R.string.soft_update_no, Toast.LENGTH_SHORT).show();
		}
		
	}
	
	private void showNoticeDialog() {
		// TODO Auto-generated method stub
		//構造對話框
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setTitle(R.string.soft_update_title);
		builder.setMessage(R.string.soft_update_info);
		//更新
		builder.setPositiveButton(R.string.soft_update_updatebtn, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				// 顯示下載對話框
				showDownloadDialog();
			}
		});
		// 稍後更新
		builder.setNegativeButton(R.string.soft_update_later, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		Dialog noticeDialog = builder.create();
		noticeDialog.show();
	}
	
	private void showDownloadDialog() {
		// 構造軟件下載對話框
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setTitle(R.string.soft_updating);
		// 给下載對話框增加進度條
		final LayoutInflater inflater = LayoutInflater.from(mContext);
		View view = inflater.inflate(R.layout.softupdate_progress, null);
		mProgressBar = (ProgressBar) view.findViewById(R.id.update_progress);
		builder.setView(view);
		builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				// 設置取消狀態
				cancelUpdate = true;
			}
		});
		mDownloadDialog = builder.create();
		mDownloadDialog.show();
		//下載文件
		downloadApk();
	}
	
	/**
	 * 下載APK文件
	 */
	private void downloadApk() {
		// TODO Auto-generated method stub
		// 启動新線程下載軟件
		new DownloadApkThread().start();
	}


	/**
	 * 檢查軟件是否有更新版本
	 * @return
	 */
	public boolean isUpdate() {
		// 獲取當前軟件版本
		int versionCode = getVersionCode(mContext);
		//把version.xml放到網络上,然後獲取文件信息
		InputStream inStream = ParseXmlService.class.getClassLoader().getResourceAsStream("version.xml");
		// 解析XML文件。 由於XML文件比較小,因此使用DOM方式進行解析
		ParseXmlService service = new ParseXmlService();
		try {
			mHashMap = service.parseXml(inStream);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if(null != mHashMap) {
			int serviceCode = Integer.valueOf(mHashMap.get("version"));
			//版本判斷
			if(serviceCode > versionCode) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 獲取軟件版本號
	 * @param context
	 * @return
	 */
	private int getVersionCode(Context context) {
		// TODO Auto-generated method stub
		int versionCode = 0;

		// 獲取軟件版本號,對應AndroidManifest.xml下android:versionCode
		try {
			versionCode = context.getPackageManager().getPackageInfo(
					"com.xiaowu.news", 0).versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionCode;
	}
	
	/**
	 * 下載文件線程
	 * @author Administrator
	 *
	 */
	private class DownloadApkThread extends Thread {
		@Override
		public void run() {
			try
			{
				//判斷SD卡是否存在,並且是否具有讀寫權限
				if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
				{
					// 獲取SDCard的路徑
					String sdpath = Environment.getExternalStorageDirectory() + "/";
					mSavePath = sdpath + "download";
					URL url = new URL(mHashMap.get("url"));
					// 創建連接
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.connect();
					// 獲取文件大小
					int length = conn.getContentLength();
					// 創建輸入流
					InputStream is = conn.getInputStream();

					File file = new File(mSavePath);
					// 如果文件不存在,新建目錄
					if (!file.exists())
					{
						file.mkdir();
					}
					File apkFile = new File(mSavePath, mHashMap.get("name"));
					FileOutputStream fos = new FileOutputStream(apkFile);
					int count = 0;
					// 緩存
					byte buf[] = new byte[1024];
					// 寫入到文件中
					do
					{
						int numread = is.read(buf);
						count += numread;
						// 計算進度條的位置
						progress = (int) (((float) count / length) * 100);
						// 更新進度
						mHandler.sendEmptyMessage(DOWNLOAD);
						if (numread <= 0)
						{
							// 下載完成
							mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
							break;
						}
						// 寫入文件
						fos.write(buf, 0, numread);
					} while (!cancelUpdate);//點擊取消就停止下載
					fos.close();
					is.close();
				}
			} catch (MalformedURLException e)
			{
				e.printStackTrace();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			// 取消下載對話框顯示
			mDownloadDialog.dismiss();
		}
	}
	
	/**
	 * 安裝APK文件
	 */
	private void installApk()
	{
		File apkfile = new File(mSavePath, mHashMap.get("name"));
		if (!apkfile.exists())
		{
			return;
		}
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
		mContext.startActivity(i);
	}
}

 

 

 

 

 

 


From:CSDN

    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()