获取应用程序版本号的代码:
//用PackageManager拿到PackageInfo,PackageInfo中的versionName
PackageInfo packinfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
String version = packinfo.versionName;
获取服务器版本号的代码:
//获取服务器地址
String path = getResources().getString(R.string.url);
URL url = new URL(path);
//创建网络连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//发出请求,获得返回码
int code = conn.getResponseCode();
if(code ==200){
//获取服务器返回的流并进行解析
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
//转化为json并解析出版本号
JSONObject json = new JSONObject(result);
String serverVersion = json.getString("version");
Log.i(TAG,"服务器版本:"+serverVersion);
}
将流转化为字符串的代码:
public static String readStream(InputStream is) throws IOException{
//ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型数组的缓冲区,缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()和 toString()获取数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
return baos.toString();
}
弹出对话框的代码:
AlertDialog.Builder builder = new Builder(this);
builder.setCancelable(false);
builder.setTitle("升级提醒");
builder.setMessage(desc);
builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which) {
pd = new ProgressDialog(SplashActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
HttpUtils http = new HttpUtils();
File sdDir = Environment.getExternalStorageDirectory();
File file = new File(sdDir, SystemClock.uptimeMillis()+".apk");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
http.download(downloadpath, file.getAbsolutePath(), new RequestCallBack<File>(){
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(SplashActivity.this, "下载失败", 0).show();
pd.dismiss();
loadMainUI();
}
public void onLoading(long total, long current, boolean isUploading) {
pd.setMax((int) total);
pd.setProgress((int) current);
};
public void onSuccess(ResponseInfo<File> fileinfo) {
pd.dismiss();
Toast.makeText(SplashActivity.this, "下载成功", 0).show();
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri
.fromFile(fileinfo.result),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}else {
Toast.makeText(SplashActivity.this, "SDcard不可用,不可以自动更新", 0).show();
loadMainUI();
}
}
});
builder.setNegativeButton("稍后再说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
loadMainUI();
}
});
builder.show();
}
下载apk的操作:
1.使用开源框架xUtils
2.使用HttpUtils的download方法,填入三个参数:服务器下载地址,手机中的存储位置、回调事件
3.回调事件中有三个常用的方法:onSuccess下载成功、onFailure下载失败、onLoading更新下载进度
安装apk的代码:
1.调用系统的安装apk的界面,传入对应的参数
2.具体实现方式
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(
Uri.fromFile(fileinfo.result),
"application/vnd.android.package-archive");
startActivity(intent);