1.从服务器获取最新App的版本号和下载地址
//本人用的是Bmob的后台傻瓜式很简单,官网有详细文档 http://www.bmob.cn/
private void checkUpdate() {
Log.i("versioncode-", "checkUpdate");
BmobQuery<UpdateBean> query = new BmobQuery<UpdateBean>();
//执行查询方法
query.findObjects(new FindListener<UpdateBean>() {
@Override
public void done(List<UpdateBean> object, BmobException e) {
if (e == null) {
getverSonCode = object.get(0).getVersonCodeCloud();
downloadUrl = object.get(0).getDownloadUrl();
compareVersionCode();
} else {
Log.e("versioncode-", getverSonCode + "");
}
}
});
}
2.与本地App版本号进行比较
private void compareVersionCode() {
int systemVersionCode = VersonUtils.getVersionCode(this);
if (systemVersionCode < getverSonCode) {
showUpdateDialog();//如果本地app版本号小于服务器app版本号,显示提示更新的dialog
}
}
3.显示有新版本的对话框.
**
* 这是兼容的 AlertDialog
*/
private void showUpdateDialog() {
/*
这里使用了 android.support.v7.app.AlertDialog.Builder
可以直接在头部写 import android.support.v7.app.AlertDialog
那么下面就可以写成 AlertDialog.Builder
*/
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("有新版本啦");
builder.setMessage("优化了诸多bug");
builder.setNegativeButton("下次再说吧", null);
builder.setPositiveButton("我要更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
downloadApk();//开启线程下载App
}
});
builder.show();
}
4.开启线程下载安装包
private void downloadApk() {
View progressView = View.inflate(MainActivity.this, R.layout.download_dialog_view, null);
final ProgressBar pbDownload = (ProgressBar) progressView.findViewById(R.id.pb_downlaod);
final TextView tvPersend = (TextView) progressView.findViewById(R.id.tv_persent);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("正在下载");
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(progressView);
final AlertDialog progressshow = builder.show();
final NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(0);
OkHttpUtils//
.get()//
.url(downloadUrl)//
.build()//
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "jianwen.apk")//
{
@Override
public void onError(okhttp3.Call call, Exception e, int id) {
}
@Override
public void onResponse(File response, int id) {
}
@Override
public void inProgress(float progress, long total, int id) {
super.inProgress(progress, total, id);
Log.e(TAG, "progress" + total + "+" + progress);
pbDownload.setProgress((int) progress);
pbDownload.setMax((int) total);
tvPersend.setText(numberFormat.format(progress * 100) + "%");
if ("100".equals(numberFormat.format(progress * 100))) {
ToastUtils.showShortToast(MainActivity.this, "下载完成");
progressshow.dismiss();
openApk(MainActivity.this);
isFirstLoadInstall = false;
}
}
});
}
download_dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ProgressBar
android:layout_margin="@dimen/app_margin_right"
android:id="@+id/pb_downlaod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
<TextView
android:id="@+id/tv_persent"
android:layout_marginRight="@dimen/app_margin_right"
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15%"
android:textSize="@dimen/small_text_size"
android:textColor="@color/black"
/>
</LinearLayout>
5.弹出系统安装界面开始安装
/**
* 打开已经安装好的apk
*/
private void openApk(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "jianwen.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}