1.服务器的json文件
版本名称:versionName
版本号码:versionCode
版本描述:description
下载地址:https://..
2.更新弹出窗
/**
* 弹出对话框,提示用户更新
*/
protected void showUpdateDialog() {
//对话框,是依赖于activity存在的
Builder builder = new AlertDialog.Builder(this);
//设置左上角图标
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("版本更新");
//设置描述内容
builder.setMessage(mVersionDes);
//积极按钮,立即更新
builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//下载apk,apk链接地址,downloadUrl
downloadApk();
}
});
builder.setNegativeButton("稍后再说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消对话框,进入主界面
enterHome();
}
});
//点击取消事件监听
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//即使用户点击取消,也需要让其进入应用程序主界面
enterHome();
dialog.dismiss();
}
});
builder.show();
}
3.下载apk
protected void downloadApk() {
//apk下载链接地址,放置apk的所在路径
//1,判断sd卡是否可用,是否挂在上
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//2,获取sd路径
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+File.separator+"mobilesafe74.apk";
//3,发送请求,获取apk,并且放置到指定路径
HttpUtils httpUtils = new HttpUtils();
//4,发送请求,传递参数(下载地址,下载应用放置位置)
httpUtils.download(mDownloadUrl, path, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
//下载成功(下载过后的放置在sd卡中apk)
Log.i(tag, "下载成功");
File file = responseInfo.result;
//提示用户安装
installApk(file);
}
@Override
public void onFailure(HttpException arg0, String arg1) {
Log.i(tag, "下载失败");
//下载失败
}
//刚刚开始下载方法
@Override
public void onStart() {
Log.i(tag, "刚刚开始下载");
super.onStart();
}
//下载过程中的方法(下载apk总大小,当前的下载位置,是否正在下载)
@Override
public void onLoading(long total, long current,boolean isUploading) {
Log.i(tag, "下载中........");
Log.i(tag, "total = "+total);
Log.i(tag, "current = "+current);
super.onLoading(total, current, isUploading);
}
});
}
}
4.安装apk
/**
* 安装对应apk
* @param file 安装文件
*/
protected void installApk(File file) {
//系统应用界面,源码,安装apk入口
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
/*//文件作为数据源
intent.setData(Uri.fromFile(file));
//设置安装的类型
intent.setType("application/vnd.android.package-archive");*/
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
// startActivity(intent);
startActivityForResult(intent, 0);
}
5.检查是否有新版本
/**
* 检测版本号
*/
private void checkVersion() {
new Thread(){
public void run() {
//发送请求获取数据,参数则为请求json的链接地址
//http://192.168.13.99:8080/update74.json 测试阶段不是最优
//仅限于模拟器访问电脑tomcat
Message msg = Message.obtain();
long startTime = System.currentTimeMillis();
try {
//1,封装url地址
URL url = new URL("http://10.0.2.2:8080/update74.json");
//2,开启一个链接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3,设置常见请求参数(请求头)
//请求超时
connection.setConnectTimeout(2000);
//读取超时
connection.setReadTimeout(2000);
//默认就是get请求方式,
// connection.setRequestMethod("POST");
//4,获取请求成功响应码
if(connection.getResponseCode() == 200){
//5,以流的形式,将数据获取下来
InputStream is = connection.getInputStream();
//6,将流转换成字符串(工具类封装)
String json = StreamUtil.streamToString(is);
Log.i(tag, json);
//7,json解析
JSONObject jsonObject = new JSONObject(json);
//debug调试,解决问题
String versionName = jsonObject.getString("versionName");
mVersionDes = jsonObject.getString("versionDes");
String versionCode = jsonObject.getString("versionCode");
mDownloadUrl = jsonObject.getString("downloadUrl");
//日志打印
Log.i(tag, versionName);
Log.i(tag, mVersionDes);
Log.i(tag, versionCode);
Log.i(tag, mDownloadUrl);
//8,比对版本号(服务器版本号>本地版本号,提示用户更新)
if(mLocalVersionCode<Integer.parseInt(versionCode)){
//提示用户更新,弹出对话框(UI),消息机制
msg.what = UPDATE_VERSION;
}else{
//进入应用程序主界面
msg.what = ENTER_HOME;
}
}
}catch(MalformedURLException e) {
e.printStackTrace();
msg.what = URL_ERROR;
}catch (IOException e) {
e.printStackTrace();
msg.what = IO_ERROR;
} catch (JSONException e) {
e.printStackTrace();
msg.what = JSON_ERROR;
}finally{
//指定睡眠时间,请求网络的时长超过4秒则不做处理
//请求网络的时长小于4秒,强制让其睡眠满4秒钟
long endTime = System.currentTimeMillis();
if(endTime-startTime<4000){
try {
Thread.sleep(4000-(endTime-startTime));
} catch (Exception e) {
e.printStackTrace();
}
}
mHandler.sendMessage(msg);
}
};
}.start();
/*new Thread(new Runnable() {
@Override
public void run() {
}
});*/
}
6.取消安装回调
//开启一个activity后,返回结果调用的方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
enterHome();
super.onActivityResult(requestCode, resultCode, data);
}