WEB开发网
开发学院手机开发Android 开发 android自动更新软件版本 阅读

android自动更新软件版本

 2013-08-16 15:35:35 来源:WEB开发网   
核心提示:private final int DOWNLOAD_OVER = 2; //标记下载完成 private final String TAG="版本更新"; //日志打印标签 private Handler mhandler = n
private final int DOWNLOAD_OVER = 2; //标记下载完成
private final String TAG="版本更新";
//日志打印标签
private Handler mhandler = new Handler() { //更新UI的handler

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case DOWNLOAD_ING:
// 更新进度条
mProgressBar.setProgress(progress);
break;
case DOWNLOAD_OVER:
downloadDialog.dismiss();
installApk();
//安装
break;
default:
break;
}
}

};

/*
* 构造方法
*/
public UpdateManager(Context context) {
this.mContext = context;
}

/*
* 检查是否有需要更新,具体比较版本xml
*/
public void checkUpdate() {
// 到服务器检查软件是否有新版本
//如果有则
showNoticeDialog();
}

/*
* 显示版本更新对话框
*/
private void showNoticeDialog() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("版本更新");
builder.setMessage(updateMsg);
builder.setPositiveButton("更新", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
noticeDialog.dismiss();
showDownloadDialog();
}
});
builder.setNegativeButton("以后再说", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
noticeDialog.dismiss();
}
});
noticeDialog = builder.create();
noticeDialog.show();

}

/*
* 弹出下载进度对话框
*/
private void showDownloadDialog() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgressBar = (ProgressBar) v.findViewById(R.id.updateProgress);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
downloadDialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();
downloadLatestVersionApk();

}

/*
* 下载最新的apk文件
*/
private void downloadLatestVersionApk() {
downloadApkThread = new Thread(downloadApkRunnable);
downloadApkThread.start();
}

//匿名内部类,apk文件下载线程
private Runnable downloadApkRunnable = new Runnable() {

public void run() {
try {
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5*1000);
conn.connect();
int length = conn.getContentLength();
Log.e(TAG, "总字节数:"+length);

Tags:android 自动 更新

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接