现在很多第三方都提供应用升级的集成,可能只需要把jar包导入,然后加入几行代码就可以了,但是这样的使用不利于我们对Android的理解。废话不多说,先讲讲怎么实现吧!
1、检查版本号
获取本地版本号:
public static String getVersionCode(Context context){
PackageManager packageManager = context.getPackageManager();//获取包管理器,用于后面获取应用信息
try {
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);//获取本应用信息
return packageInfo.versionCode+"";
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
获取网络上最新版本号:
//获取最新版本信息,判断是否需要升级
public void getCloudVersion(){
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url = new URL(GlobalURL.QUERYVERSION);//我的应用检查版本的地址Url
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//打开一个网络连接
connection.setRequestMethod("POST");//设置请求method为post
DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打开一个输出流
out.writeBytes("imei="+CommonUtil.getDeviceID(context)+"&soundNo=1");//需要提交给后台服务器的数据参数之间用&分割
InputStream inputStream = connection.getInputStream();//获取输入流,服务器响应内容
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder builder = new StringBuilder();//把输入流包装成字符缓冲输入流,有人可能会问为什么要用buffer,buffer是为了使两个速度差异很大的端之间的交流不会因此而出现问题
String line;
while((line=bufferedReader.readLine()) != null){
builder.append(line);
}
JSONObject jsonObject = new JSONObject(builder.toString());
JSONObject jsonObject1 = jsonObject.getJSONObject("data");
apkUrl = jsonObject1.getString("apkUrl");
version = jsonObject1.getString("version");
content = jsonObject1.getString("content");
if (Integer.parseInt(version)>Integer.parseInt(getVersionCode(context))) {
mHandler.sendEmptyMessage(SHOW_DOWNLOAD_DIALOG);//判断到需要升级,则显示升级提示框
}
//下面是一个检查最新版本api返回的一个json例子 //{"success":true,"code":"1","data":{"apkUrl":"http://192.168.63.4:8082/orange-web/res/apkfile/1463894203012422.apk","version":1,"content":"更新说明"}}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}.start();
}
2、询问是否升级
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SHOW_DOWNLOAD_DIALOG:
showDownloadDialog();
break;
default:
break;
}
}
};
//显示对话框,询问是否更新
public void showDownloadDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("检测到新版本 "+version);
builder.setMessage(content);
builder.setCancelable(false);
builder.setPositiveButton("升级",new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
initProgressDialog();
}
});
builder.setNegativeButton("暂不升级", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.show();
}
3、初始化关键控件开始下载更新
//初始化下载进度条
public void initProgressDialog(){
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("准备下载...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
httpHandler.cancel();
}
});
progressDialog.show();
downloadNewApk();
}
//下载更新apk
public void downloadNewApk() {
HttpUtils httpUtils = new HttpUtils();
httpHandler = httpUtils.download(apkUrl, Environment.getExternalStorageDirectory()+"/Download/tingcheng.apk", new RequestCallBack() {
@Override
public void onLoading(long total, long current,
boolean isUploading) {
progressDialog.setMessage("正在下载...");
progressDialog.setProgress((int)current);
progressDialog.setMax((int)total);
progressDialog.setProgressNumberFormat(current/1024+"K"+"/"+total/1024+"K");
}
@Override
public void onSuccess(ResponseInfo arg0) {
// TODO Auto-generated method stub
progressDialog.dismiss();
installAPK();
}
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
progressDialog.setMessage("下载失败");
}
});
}
不需要使用第三方的升级库,自己就可以做了 : )