android 检查软件是否有更新版本
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import com.yuxin.mhealth.R;
import com.yuxin.mhealth.http.action.BaseAction;
import com.yuxin.mhealth.ui.BaseActivity;
import com.yuxin.mhealth.utils.DialogUtil;
import com.yuxin.mhealth.utils.SysConstact;
import com.yuxin.mhealth.utils.SysDownloadUtils;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Message;
/**
* @description 软件更新
*/
public class UpdateManagerActivity extends BaseActivity {
/* 保存解析的XML信息 */
HashMap<String, String> mHashMap;
private Context mContext;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x003:
closeProgressDialog();
// 显示新版本更新对话框
showNoticeDialog();
break;
case 0x004:
doProgressDialog("当前已经是最新版本!");
closeProgressDialog();
break;
case 0x005:
doProgressDialog("检查更新失败!");
closeProgressDialog();
break;
default:
break;
}
};
};
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
};
/**
* 检测软件更新
*/
public void checkUpdate() {
switch (isUpdate()) {
case 0:
mHandler.sendEmptyMessage(0x005);
break;
case 1:
// 显示提示对话框
mHandler.sendEmptyMessage(0x003);
break;
case 2:
mHandler.sendEmptyMessage(0x004);
break;
default:
break;
}
}
/**
* 检查软件是否有更新版本
*
* @return
* @throws Exception
*/
private int isUpdate() {
// 获取当前软件版本
int versionCode = getVersionCode(mContext);
URL url;
// 获取,解析XML文件。 由于XML文件比较小,因此使用DOM方式进行解析
ParseXmlService service = new ParseXmlService();
try {
url = new URL(BaseAction.getUploadUrl(this));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
mHashMap = service.parseXml(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
if (null != mHashMap) {
SysConstact.Log("版本" + mHashMap.get("version"));
int serviceCode = Integer.valueOf(mHashMap.get("version").trim());
// 版本判断
if (serviceCode > versionCode) {
return 1;
} else {
return 2;
}
}
return 0;
}
/**
* 获取软件版本号
*
* @param context
* context
* @return
*/
private int getVersionCode(Context context) {
int versionCode = 0;
try {
String packageName = context.getPackageName();
// 获取软件版本号,对应AndroidManifest.xml下android:versionCode
versionCode = context.getPackageManager().getPackageInfo(
packageName, 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 显示软件更新对话框
*/
private void showNoticeDialog() {
// 构造对话框
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) {
// 显示下载对话框
DialogUtil.toast(getApplicationContext(), "下载已经开始,可以在通知栏查看进度!");
SysDownloadUtils.start(getApplicationContext(), mHashMap.get("url").trim(),mHashMap.get("name").trim());
dialog.dismiss();
}
});
// 稍后更新
builder.setNegativeButton(R.string.soft_update_later,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Dialog noticeDialog = builder.create();
noticeDialog.show();
}
}