本文所述实例为一个天气预报中的android代码,主要包括了下载和安装APK、检查Apk更新、显示'已经是最新'或者'无法获取版本信息'对话框、获取当前客户端版本信息、显示版本更新通知对话框、显示下载对话框、判断是否挂载了SD卡、显示文件大小格式:2个小数点显示等。具体实现代码如下:
import java.io.ByteArrayInputStream; 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.text.DecimalFormat; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; import org.lmw.weather.R; import org.lmw.weather.entity.AppDetail; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.pm.PackageInfo; 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.TextView; import android.widget.Toast; public class DownloadManager { private static final int DOWN_NOSDCARD = 0; private static final int DOWN_UPDATE = 1; private static final int DOWN_OVER = 2; private static final int DOWN_ERROR=3; private static final int DIALOG_TYPE_LATEST = 0; private static final int DIALOG_TYPE_FAIL = 1; private static final int DIALOG_TYPE_INTERNETERROR = 2; private static DownloadManager downloadManager; private Context mContext; // 通知对话框 private Dialog noticeDialog; // 下载对话框 private Dialog downloadDialog; // 进度条 private ProgressBar mProgress; // 显示下载数值 private TextView mProgressText; // 查询动画 private ProgressDialog mProDialog; // '已经是最新' 或者 '无法获取最新版本' 的对话框 private Dialog latestOrFailDialog; // 返回的安装包url private String apkUrl = ""; // 进度值 private int progress; // 下载线程 private Thread downLoadThread; // 终止标记 private boolean interceptFlag; // 提示语 private String updateMsg = ""; // 下载包保存路径 private String savePath = ""; // apk保存完整路径 private String apkFilePath = ""; // 临时下载文件路径 private String tmpFilePath = ""; // 下载文件大小 private String apkFileSize; // 已下载文件大小 private String tmpFileSize; private String curVersiOnName= ""; private int curVersionCode; private AppDetail mDownload; private String checkUrl="http://192.168.0.133:8080/lmw/androidMarket/SimpleWeather-20130701093349937/update.xml"; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case DOWN_UPDATE: mProgress.setProgress(progress); mProgressText.setText(tmpFileSize + "/" + apkFileSize); break; case DOWN_OVER: downloadDialog.dismiss(); installApk(); break; case DOWN_NOSDCARD: downloadDialog.dismiss(); Toast.makeText(mContext, "无法下载安装文件,请检查SD卡是否挂载",Toast.LENGTH_SHORT).show(); break; case DOWN_ERROR: downloadDialog.dismiss(); if(msg.arg1==0){ Toast.makeText(mContext, "网络不给力啊", Toast.LENGTH_SHORT).show(); }else if(msg.arg1==1||msg.arg1==2){ Toast.makeText(mContext, "未找到资源",Toast.LENGTH_SHORT).show(); } break; } }; }; public static DownloadManager getDownloadManager() { if (downloadManager == null) { downloadManager = new DownloadManager(); } downloadManager.interceptFlag = false; return downloadManager; } public void DownLoader(Context context, AppDetail download) { this.mCOntext= context; this.mDownload = download; showDownloadDialog(); } /** * 检查App更新 * @param context * @param isShowMsg * 是否显示提示消息 */ public void checkAppUpdate(Context context, final boolean isShowMsg,final boolean notmain) { this.mCOntext= context; getCurrentVersion(mContext); if (isShowMsg) { if (mProDialog == null) mProDialog = ProgressDialog.show(mContext, null, "正在检测,请稍后...",true, true); else if (mProDialog.isShowing()|| (latestOrFailDialog != null && latestOrFailDialog.isShowing())) return; } final Handler handler = new Handler() { public void handleMessage(Message msg) { // 进度条对话框不显示 - 检测结果也不显示 if (mProDialog != null && !mProDialog.isShowing()) { return; } // 关闭并释放释放进度条对话框 if (isShowMsg && mProDialog != null) { mProDialog.dismiss(); mProDialog = null; } // 显示检测结果 if (msg.what == 1) { mDownload = (AppDetail) msg.obj; if (mDownload != null) { if (curVersionCode