1. 获取本地的VersionCode 这这里呢 封装成了一个类(可自提)
public class APKVersionCodeUtils {
/**
*获取本地版本号
*
*/
public static int getVersionCode(Context mContext) {
int versionCode = 0;
try {
//获取软件版本号,对应AndroidManifest.xml下android:versionCode
versionCode = mContext.getPackageManager().
getPackageInfo(mContext.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 获取版本号名称
*
* @param context 上下文
* @return
*/
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().
getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
}
2.网络请求后台接口做判断 判断你的版本号是否低于接口返回的版本号
这个地方我相信大家都会 就不过多写了
3.在我们的程序需要版本更新的时候 要用AlertDiaLog提示
AlertDiaLog提示完以后,如果用户点击下载, 我们要做下载最新Apk的任务
说不多说,上代码
public void loadNewVersionAlertDiaLog() {
AlertDialog.Builder alert = new AlertDialog.Builder(ShowActivity.this);
alert.setTitle("说明");
alert.setMessage("发现新版本,是否立即更新?");
alert.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadApk();
}
});
alert.setNegativeButton("暂不更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.create();
alert.show();
}
/**
* 从服务器端下载最新apk
*/
private void downloadApk() {
//显示下载进度
ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.show();
new Thread(new DownloadApk(dialog)).start();
}
/**
* 访问网络下载apk
*/
private class DownloadApk implements Runnable {
private ProgressDialog dialog;
InputStream is;
FileOutputStream fos;
private String TAG;
public DownloadApk(ProgressDialog dialog) {
this.dialog = dialog;
}
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
String url = 从后台接口中获取的接口Apk字段;
Request request = new Request.Builder().get().url(url).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Log.d(TAG, "开始下载apk");
//获取内容总长度
long contentLength = response.body().contentLength();
//设置最大值
dialog.setMax((int) contentLength);
//保存到sd卡
File apkFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".apk");
fos = new FileOutputStream(apkFile);
//获得输入流
is = response.body().byteStream();
//定义缓冲区大小
byte[] bys = new byte[1024];
int progress = 0;
int len = -1;
while ((len = is.read(bys)) != -1) {
try {
Thread.sleep(1);
fos.write(bys, 0, len);
fos.flush();
progress += len;
//设置进度
dialog.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//下载完成,提示用户安装
installApk(apkFile);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭io流
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
fos = null;
}
}
dialog.dismiss();
}
}