(1)获取当前App的版本。
PackageInfo pinfo = null;
try {
pinfo = getPackageManager().getPackageInfo(
App.this.getPackageName(),
PackageManager.GET_CONFIGURATIONS);
m_versionCode = pinfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
(2)使用HttpClient去服务器获取APP的最新版本号,比较APP的最新版本号。如果相同,则不用提示更新,退出该流程
final JSONObject obj = JSON.parseObject(response);
if (obj.getInteger("version") == m_versionCode) {
if (callback != null) {
callback.noUpdate();
}
return;
}
(4)弹出对话,要用户选择是否需要更新APP?如果选择“确定”,则去服务器下载安装包,且弹出progressDialog更新窗口如果选择“取消”,退出该流程</p><p><pre
Builder builder = new Builder(context);
builder.setTitle("检测到新版本");
final String versionName = versionCodeToName(obj
.getInteger("version"));
builder.setMessage("版本: " + versionName + "\n大小: "
+ converToSuitableSize(obj.getInteger("size"))
+ "\n是否更新?");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
m_updatePd = createUpdateDialog(context);
Thread updateThread = new Thread(
new Runnable() {
@Override
public void run() {
URL url = null;
HttpURLConnection con = null;
String appSaveName = "kanxue_"
+ versionName + ".apk";
FileOutputStream fos = null;
try {
url = new URL(obj
.getString("url"));
con = (HttpURLConnection) url
.openConnection();
int filelength = con
.getContentLength();
if (Environment
.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)) {
String Path = Environment
.getExternalStorageDirectory()
+ "/kanxue/tmp";
File file = new File(
Path);
if (!file.exists()) {
file.mkdirs();
}
m_appSavePath = Path
+ "/"
+ appSaveName;
file = new File(
m_appSavePath);
if (file.exists()) {
file.delete();
}
file.createNewFile();
fos = new FileOutputStream(
file);
} else {
fos = openFileOutput(
appSaveName,
MODE_PRIVATE);
m_appSavePath = App.this
.getFilesDir()
+ "/"
+ appSaveName;
}
if (filelength > 0) {
m_updatePd
.setMax(filelength);
InputStream input = con
.getInputStream();
// 读取大文件
byte[] buffer = new byte[4 * 1024];
int len = input
.read(buffer);
int downlength = 0;
while (len != -1) {
fos.write(buffer,
0, len);
downlength += len;
len = input
.read(buffer);
if (m_bCancel) {
m_handler
.sendEmptyMessage(-1);
break;
} else {
m_handler
.sendEmptyMessage(downlength);
}
}
fos.flush();
}
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO 自动生成的 catch
// 块
e.printStackTrace();
}
}
if (con != null) {
con.disconnect();
}
}
}
});
m_updatePd.show();
updateThread.start();
}
}).setNegativeButton("取消", null);
builder.create().show();
(5)安装下载好的APK程序包
Uri uri = Uri.fromFile(new File(path));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
(6)流程结束