Android apps应用检查更新代码
JAVA代码:
UpdateManager.java
[java]
<pre name="code" class="java">package com.iteye.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 android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.content.DialogInte易做图ce.OnClickListener;
import android.content.Intent;
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;
import com.iteye.androidtoast.R;
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 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:
// 安装文件
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();
}
}
/**
* 检查软件是否有更新版本
*
* @return
*/
private boolean isUpdate()
{
// 获取当前软件版本
int versionCode = getVersionCode(mContext);
// 把version.xml放到网络上,然后获取文件信息
//InputStream inStream = ParseXmlService.class.getClassLoader().getResourceAsStream("version.xml");
// 解析XML文件。 由于XML文件比较小,因此使用DOM方式进行解析
ParseXmlService service = new ParseXmlService();
try
{
String path = "http://192.168.1.146:8080/picweb/xml/version.xml"; //地址是服务器上version.xml链接地址
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
mHashMap = service.parseXml(inStream);
} catch (Exception e)
{
e.printStackTrace();
}
if (null != mHashMap)
{
int serviceCode = Integer.valueOf(mHashMap.get("version"));
// 版本判断
&nb
补充:移动开发 , Android ,