public class UpdateManager {
// 外存sdcard存放路径
private static final String FILE_PATH = Environment.getExternalStorageDirectory() +"/" + "AutoUpdate" +"/";
// 下载应用存放全路径
private static final String FILE_NAME = FILE_PATH + "AutoUpdate.apk";
// 准备安装新版本应用标记
private static final int INSTALL_TOKEN = 1;
//Log日志打印标签
private static final String TAG = "Update_log";
private Context context;
//获取版本数据的地址
private String version_path = "http://?????";
//获取新版APK的默认地址
private String apk_path = "http://??????";
// 下载应用的进度条
private ProgressDialog progressDialog;
//新版本号和描述语言
private int update_versionCode;
private String update_describe;
public UpdateManager(Context context) {
this.context = context;
}
/**
* 获取当前版本号
*/
private int getCurrentVersion() {
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
Log.e(TAG, "当前版本名和版本号" + info.versionName + "--" + info.versionCode);
return info.versionCode;
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "获取当前版本号出错");
return 0;
}
}
/**
* 从服务器获得更新信息
*/
public void getUpdateMsg() {
class mAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
try {
URL url_version = new URL(params[0]);
connection = (HttpURLConnection) url_version.openConnection();
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));
Log.e(TAG, "bufferReader读到的数据--" + reader);
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String s) { //回到主线程,更新UI
Log.e(TAG, "异步消息处理反馈--" + s);
try {
JSONObject object = new JSONObject(s);
update_versionCode = object.getInt("versionCode");
update_describe = object.getString("describe");
Log.e(TAG, "新版本号--" + update_versionCode);
Log.e(TAG, "新版本描述--\n" + update_describe);
} catch (JSONException e) {
e.printStackTrace();
}
if (update_versionCode > getCurrentVersion()) {
Log.e(TAG, "提示更新!");
showNoticeDialog();
} else {
Log.e(TAG, "已是最新版本!");
}
}
}
new mAsyncTask().execute(version_path);
}
/**
* 显示提示更新对话框
*/
private void showNoticeDialog() {
new AlertDialog.Builder(context)
.setTitle("检测到新版本!")
.setMessage(update_describe)
.setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
showDownloadDialog();
}
}).setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
/**
* 显示下载进度对话框
*/
public void showDownloadDialog() {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("正在下载...");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new downloadAsyncTask().execute();
}
/**
* 下载新版本应用
*/
private class downloadAsyncTask extends AsyncTask<Void, Integer, Integer> {
@Override
protected void onPreExecute() {
Log.e(TAG, "执行至--onPreExecute");
progressDialog.show();
}
@Override
protected Integer doInBackground(Void... params) {
Log.e(TAG, "执行至--doInBackground");
URL url;
HttpURLConnection connection = null;
InputStream in = null;
FileOutputStream out = null;
try {
url = new URL(apk_path);
connection = (HttpURLConnection) url.openConnection();
in = connection.getInputStream();
long fileLength = connection.getContentLength();
File file_path = new File(FILE_PATH);
if (!file_path.exists()) {
file_path.mkdir();
}
out = new FileOutputStream(new File(FILE_NAME));//为指定的文件路径创建文件输出流
byte[] buffer = new byte[1024 * 1024];
int len = 0;
long readLength = 0;
Log.e(TAG, "执行至--readLength = 0");
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);//从buffer的第0位开始读取len长度的字节到输出流
readLength += len;
int curProgress = (int) (((float) readLength / fileLength) * 100);
Log.e(TAG, "当前下载进度:" + curProgress);
publishProgress(curProgress);
if (readLength >= fileLength) {
Log.e(TAG, "执行至--readLength >= fileLength");
break;
}
}
out.flush();
return INSTALL_TOKEN;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.e(TAG, "异步更新进度接收到的值:" + values[0]);
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(Integer integer) {
progressDialog.dismiss();//关闭进度条
//安装应用
installApp();
}
}
/**
* 安装新版本应用
*/
private void installApp() {
File appFile = new File(FILE_NAME);
if (!appFile.exists()) {
return;
}
// 跳转到新版本应用安装页面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + appFile.toString()), "application/vnd.android.package-archive");
context.startActivity(intent);
}
}
manager=new UpdateManager(this);
manager.getUpdateMsg();//检查更新