Preface:
In the process of project development, there is generally a need for "version update". We can judge whether to upgrade according to the version code. Generally, for each version update, the version number is increased by one. If the version number on the get server is higher than the version number of this program, you will be prompted to upgrade.
Train of thought:
1. Retrieve + broadcastreceiver to get the version number of the server
2. Get the version number of the current program
3. Check whether it is updated
4. Pop up the dialog box of update prompt
Implementation steps:
1. Add network and read-write permissions
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. Register BroadcastReceiver and check for version update
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Register for BroadcastReceiver myReceiver=new NewVersionAppReceiver(); IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(myReceiver, filter); } @Override public void onDestroy() { super.onDestroy(); //Unregister BroadcastReceiver unregisterReceiver(myReceiver); } private class NewVersionAppReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { checkNewVersionApp();//Check for version updates } } private void checkNewVersionApp(){ new Handler(getMainLooper()).post(new Runnable() {//Open a new thread and use the Retrofit interface @Override public void run() { commonPresenter = new CommonPresenter(MainActivity.this); commonPresenter.getNewVersionAPP(); } }); }
3.Retrofit implements the network request to obtain the server's version code. By comparing the server's version code with the version code of the current App application, it can determine whether to upgrade the App application or not.
//Method of successful callback for Retrofit network request 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();//Get the versionCode of the server int currentVersionCode = getCurrentVersionCode(MainActivity.this); if (serviceVersionCode > currentVersionCode) { showNewVersionAppPopWindow();//Pop up dialog } } } //Get the versionCode of the App application 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. Pop up the dialog box of update prompt
private void showNewVersionAppPopWindow() { if (!isFinishing()) { //Add this judgment to solve the problem of "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();//close dialog boxes } }); //Go to Google Play website to download 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. Code implemented in Activity
public class MainActivity extends BaseActivity{ ... private CustomPopWindow mListPopWindow2; private NewVersionAppReceiver myReceiver; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Register for BroadcastReceiver myReceiver=new NewVersionAppReceiver(); IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(myReceiver, filter); } ... @Override public void onDestroy() { super.onDestroy(); //Unregister BroadcastReceiver unregisterReceiver(myReceiver); } ... @Override public void onHttpSuccess(int reqType, Message msg) {//The method of Retrofit request network successful callback if (reqType == IHttpService.HTTP_GET_NEW_VERSION_APP) { New_version_app newVersionApp = (New_version_app) msg.obj; int serviceVersionCode = newVersionApp.getVersionCode();//Get the versionCode of the server int currentVersionCode = getCurrentVersionCode(MainActivity.this); if (serviceVersionCode > currentVersionCode) { showNewVersionAppPopWindow();//Pop up dialog } } } //BroadcastReceiver private class NewVersionAppReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { checkNewVersionApp();//Check for version updates } } //Check for version updates private void checkNewVersionApp(){ new Handler(getMainLooper()).post(new Runnable() {//Open a new thread and use the Retrofit interface @Override public void run() { commonPresenter = new CommonPresenter(MainActivity.this); commonPresenter.getNewVersionAPP(); } }); } //How to pop up a dialog box private void showNewVersionAppPopWindow() { if (!isFinishing()) { //Solve the problem of "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();//close dialog boxes } }); //Click the button and go to Google Play website to download 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. Summary: my project is to release Google Play. When there is a new version that needs to be upgraded, the client will pop up the "version update" dialog box, and the user can click the download button to directly jump to the Google Play official website to download the APK. The function of App version update has been realized. Welcome to watch. If you have any questions, please contact me with a message!