前言:
在项目开发过程中一般都有"版本更新"的需求,我们可以根据版本号(versionCode)来判断是否升级,每次版本更新,则版本号加一。如果获取服务器的版本号比当前应用程序的版本号还高,那么提示升级。弹出"版本更新"的对话框后,如果用户没有点击"DownLoad"下载新版本的APP, 那么在第一次打开APP时执行“版本更新”的网络请求时,把时间戳保存下来,以后每次打开APP获取当前时间戳,如果新旧时间戳的差不大于一天,就不执行请求,如果大于一天,则执行请求。
思路:
1.第一次打开APP时执行“版本更新”的网络请求,获取服务器的版本号
2.获取当前程序的版本号,并根据新旧版本号来检测是否更新
3.如果服务器上的版本号比当前程序的版本号高,则弹出更新提示的对话框
4.点击"Later"把时间戳保存下来,每次打开APP获取当前时间戳
5.如果新旧时间戳的差大于或等于一天,执行网络请求(直到点击"DownLoad"为止)
6.点击"DownLoad"直接到Google Play官网去下载APK就可以
实现步骤:
1.添加网络和读写权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.第一次打开APP时执行"版本更新"的网络请求
...
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
if (timeStart==0){//第一次打开APP时执行"版本更新"的网络请求
//版本更新的网络请求
}
}
3.Retrofit实现网络请求获取服务器的versionCode,通过比较服务器的versionCode跟当前APP应用程序的versionCode,来判断要不要升级APP应用程序。
//Retrofit网络请求成功回调的方法
public void onHttpSuccess(int reqType, Message msg) {
if (reqType == IHttpService.HTTP_GET_NEW_VERSION_APP) {
New_version_app newVersionApp = (New_version_app) msg.obj;
int serviceVersionCode = newVersionApp.getVersionCode();//获取服务器的versionCode
int currentVersionCode = getCurrentVersionCode(MainActivity.this);
if (serviceVersionCode > currentVersionCode) {
showNewVersionAppPopWindow();//弹出对话框
}
}
}
//获取APP应用程序的versionCode
private int getCurrentVersionCode(Context ctx) {
int currentVersion = 0;
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
currentVersion = packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return currentVersion;
}
4.如果服务器上的版本号比当前程序的版本号高,则弹出更新提示的对话框。
private void showNewVersionAppPopWindow() {
if (!isFinishing()) { //添加这个判断以解决"unable add to window"的问题
View contentView = LayoutInflater.from(this).inflate(R.layout.pop_check_versionapp, null);
LinearLayout linearLayout = (LinearLayout) contentView.findViewById(R.id.linearLayout);
TextView tvTitle = (TextView) contentView.findViewById(R.id.tv_title);
TextView tvContent = (TextView) contentView.findViewById(R.id.tv_content);
Button btnLater = (Button) contentView.findViewById(R.id.btn_later);
Button btnDownload = (Button) contentView.findViewById(R.id.btn_download);
//创建和展示popWindow
mListPopWindow2 = new CustomPopWindow.PopupWindowBuilder(this)
.setView(contentView)
.enableBackgroundDark(true)
.setBgDarkAlpha(0.4f)
.size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
.create()
.showAsDropDown(linearLayout, 0, 20);
btnLater.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListPopWindow2.dissmiss();//关闭对话框
Long timeStart = new Date().getTime();//点击”Later”把时间戳保存下来
SharedPreUtil.saveLong(Global.mContext,"timeStart",timeStart);
}
});
//跳转到Google Play官网去下载APK
btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String appPackageName = getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
});
}
}
5.在Activity实现的代码
public class MainActivity extends BaseActivity{
...
private CustomPopWindow mListPopWindow2;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//每次打开APP获取当前时间戳
long timeEnd = System.currentTimeMillis();
//获取"Later"保存的时间戳
Long timeStart = SharedPreUtil.getLong(Global.mContext, "timeStart", (long) 0);
if (timeStart==0){//第一次打开APP时执行"版本更新"的网络请求
//版本更新的网络请求
}else if (timeStart!=0 && timeEnd - timeStart >= 24*60*60*1000) {
//如果新旧时间戳的差大于或等于一天,执行网络请求
}
}
...
@Override
public void onHttpSuccess(int reqType, Message msg) {//Retrofit请求网络成功回调的方法
if (reqType == IHttpService.HTTP_GET_NEW_VERSION_APP) {
New_version_app newVersionApp = (New_version_app) msg.obj;
int serviceVersionCode = newVersionApp.getVersionCode();//获取服务器的versionCode
int currentVersionCode = getCurrentVersionCode(MainActivity.this);
if (serviceVersionCode > currentVersionCode) {
showNewVersionAppPopWindow();//弹出对话框
}
}
}
//如果服务器上的版本号比当前程序的版本号高,弹出对话框的方法
private void showNewVersionAppPopWindow() {
if (!isFinishing()) { //解决"unable to add window"问题
View contentView = LayoutInflater.from(this).inflate(R.layout.pop_check_versionapp, null);
LinearLayout linearLayout = (LinearLayout) contentView.findViewById(R.id.linearLayout);
TextView tvTitle = (TextView) contentView.findViewById(R.id.tv_title);
TextView tvContent = (TextView) contentView.findViewById(R.id.tv_content);
Button btnLater = (Button) contentView.findViewById(R.id.btn_later);
Button btnDownload = (Button) contentView.findViewById(R.id.btn_download);
//创建和展示popWindow
mListPopWindow2 = new CustomPopWindow.PopupWindowBuilder(this)
.setView(contentView)
.enableBackgroundDark(true)
.setBgDarkAlpha(0.4f)
.size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
.create()
.showAsDropDown(linearLayout, 0, 20);
btnLater.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListPopWindow2.dissmiss();//点击"Later",关闭对话框
Long timeStart = new Date().getTime();//保存当前的时间戳
SharedPreUtil.saveLong(Global.mContext,"timeStart",timeStart);
}
});
//点击按钮后,跳转到Google Play官网去下载APK
btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String appPackageName = getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
});
}
}
}
6.总结:当有新版本需要升级时,客户端会"版本更新"弹出对话框,用户点击下载的按钮直接跳转到Google Play官网去下载APK就可以。APP版本更新的功能已经实现,欢迎大家围观。如果有什么疑问的话,可以留言联系我哦!