前言:
在项目开发过程中一般都有"版本更新"的需求,我们可以根据版本号(versionCode)来判断是否升级,一般每次版本更新,版本号加一。如果获取服务器上的版本号比检测到本程序的版本号高,那么提示升级。
思路:
1.Retrofit+BroadcastReceiver获取服务器的版本号
2.获取当前程序的版本号
3.检测是否更新
4.弹出更新提示的对话框
实现步骤:
1.添加网络和读写权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.注册BroadcastReceiver, 检查版本更新
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注册BroadcastReceiver
myReceiver=new NewVersionAppReceiver();
IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(myReceiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
//反注册BroadcastReceiver
unregisterReceiver(myReceiver);
}
private class NewVersionAppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
checkNewVersionApp();//检查版本更新
}
}
private void checkNewVersionApp(){
new Handler(getMainLooper()).post(new Runnable() {//新开一条线程,走Retrofit接口
@Override
public void run() {
commonPresenter = new CommonPresenter(MainActivity.this);
commonPresenter.getNewVersionAPP();
}
});
}
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);
//Introducing a custom font style.
tvTitle.setTypeface(Const.font700);
tvContent.setTypeface(Const.font300);
btnLater.setTypeface(Const.font700);
btnDownload.setTypeface(Const.font700);
//Create and display 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();//关闭对话框
}
});
//跳转到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;
private NewVersionAppReceiver myReceiver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注册BroadcastReceiver
myReceiver=new NewVersionAppReceiver();
IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(myReceiver, filter);
}
...
@Override
public void onDestroy() {
super.onDestroy();
//反注册BroadcastReceiver
unregisterReceiver(myReceiver);
}
...
@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();//弹出对话框
}
}
}
//BroadcastReceiver
private class NewVersionAppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
checkNewVersionApp();//检查版本更新
}
}
//检查版本更新
private void checkNewVersionApp(){
new Handler(getMainLooper()).post(new Runnable() {//新开一条线程,走Retrofit接口
@Override
public void run() {
commonPresenter = new CommonPresenter(MainActivity.this);
commonPresenter.getNewVersionAPP();
}
});
}
//弹出对话框的方法
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);
//Introducing a custom font style.
tvTitle.setTypeface(Const.font700);
tvContent.setTypeface(Const.font300);
btnLater.setTypeface(Const.font700);
btnDownload.setTypeface(Const.font700);
//Create and display 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();//关闭对话框
}
});
//点击按钮后,跳转到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的,当有新版本需要升级时,客户端会"版本更新"弹出对话框,用户点击下载的按钮直接跳转到Google Play官网去下载APK就可以。App版本更新的功能已经实现,欢迎大家围观,如果有什么疑问的话,可以留言联系我哦!