<span style="font-size:18px;">/**
* Created by hzc on 16/4/20.
* 强制更新工具
*/
import android.accounts.NetworkErrorException;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.china.cx.R;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UpdateManager {
private Context mContext; //上下文
private String apkUrl = "http://***.apk"; //apk下载地址
private static final String savePath = "/sdcard/updateAPK/"; //apk保存到SD卡的路径
private static final String saveFileName = savePath + "apkName.apk"; //完整路径名
private Handler handler = new Handler();
//获取更新信息接口
private String requestUrl = "";//填写网络请求地址
private ProgressBar mProgress; //下载进度条控件
private static final int DOWNLOADING = 1; //表示正在下载
private static final int DOWNLOADED = 2; //下载完毕
private static final int DOWNLOAD_FAILED = 3; //下载失败
private int progress; //下载进度
private boolean cancelFlag = false; //取消下载标志位
private double serverVersion = 2.0; //从服务器获取的版本号
private double clientVersion = 1.0; //客户端当前的版本号
private String updateDescription = "更新描述"; //更新内容描述信息
private boolean forceUpdate = true; //是否强制更新
private AlertDialog alertDialog1, alertDialog2; //表示提示对话框、进度条对话框
/**
* 构造函数
*/
public UpdateManager(Context context) {
this.mContext = context;
}
//调用此方法检查更新,并自动下载安装包
public void check() {
new Thread(new Runnable() {
@Override
public void run() {
//从网络获取数据
final String response = getUpdateData(requestUrl);
//向Handler发送处理操作
handler.post(new Runnable() {
@Override
public void run() {
//在UI线程更新UI
if (response != null) {
showNoticeDialog(response);
}
}
});
}
}).start();
}
// 获取当前应用版本号
public int getVersionCode(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(),
PackageManager.GET_CONFIGURATIONS).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
public String getUpdateData(String url) {
HttpURLConnection conn = null;
try {
// 利用string url构建URL对象
URL mURL = new URL(url);
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(10000);
int responseCode = conn.getResponseCode();
Log.e("codecoee", responseCode + "");
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String response = getStringFromInputStream(is);
Log.e("responsejson", response);
// showNoticeDialog();
return response;
} else {
throw new NetworkErrorException("response status is " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("adsadas", e.toString());
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}</span>
<span style="font-size:18px;">
//javabean模型
public class Update {
private int code;
private int versionCode;
private String versionName;
private String download;
// public int getCode() {
// return code;
// }
//
// public String getDownload() {
// return download;
// }
//
// public String getVersionCode() {
// return versionCode;
// }
//
// public String getVersionName() {
// return versionName;
// }
}
private static String getStringFromInputStream(InputStream is)
throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 模板代码 必须熟练
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码)
os.close();
return state;
}
/**
* 显示更新对话框
*/
public void showNoticeDialog(String response) {
Gson gson = new Gson();
Update update = gson.fromJson(response, Update.class);
this.updateDescription = update.versionName;
this.serverVersion = update.versionCode;
this.apkUrl = update.download;
clientVersion = getVersionCode(mContext);
Log.e("clientVersion", clientVersion + "");
//如果版本最新,则不需要更新
if (serverVersion <= clientVersion)
return;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("发现新版本 :" + serverVersion);
dialog.setMessage(updateDescription);
dialog.setPositiveButton("现在更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
showDownloadDialog();
}
});
//是否强制更新
if (forceUpdate == false) {
dialog.setNegativeButton("待会更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
}
});
}
alertDialog1 = dialog.create();
alertDialog1.setCancelable(false);
alertDialog1.show();
}
/**
* 显示进度条对话框
*/
public void showDownloadDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("正在更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
dialog.setView(v);
//如果是强制更新,则不显示取消按钮
if (forceUpdate == false) {
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
cancelFlag = false;
}
});
}
alertDialog2 = dialog.create();
alertDialog2.setCancelable(false);
alertDialog2.show();
//下载apk
downloadAPK();
}
/**
* 下载apk的线程
*/
public void downloadAPK() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
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(DOWNLOADING);
if (numread <= 0) {
//下载完成通知安装
mHandler.sendEmptyMessage(DOWNLOADED);
break;
}
fos.write(buf, 0, numread);
} while (!cancelFlag); //点击取消就停止下载.
fos.close();
is.close();
} catch (Exception e) {
mHandler.sendEmptyMessage(DOWNLOAD_FAILED);
e.printStackTrace();
}
}
}).start();
}
/**
* 更新UI的handler
*/
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case DOWNLOADING:
mProgress.setProgress(progress);
break;
case DOWNLOADED:
if (alertDialog2 != null)
alertDialog2.dismiss();
installAPK();
break;
case DOWNLOAD_FAILED:
Toast.makeText(mContext, "网络断开,请稍候再试", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
};
/**
* 下载完成后自动安装apk
*/
public void installAPK() {
File apkFile = new File(saveFileName);
if (!apkFile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(intent);
}
}</span>