top of page
Writer's pictureCodersarts AI

Update Android Application if new version available

Help of play core library provided by google.

  • Check for update availability.

  • Start an update

  • Get a callback for update status

  • Handle a flexible update

  • Install a flexible update

  • Handle an immediate update




First add Library firebase remote config


we create in-app default values that control the behavior and appearance of our app

This is Available for version 4.1 of android if you are using another version of android studio check the newer version of android studio.

implementation 'com.google.firebase:firebase-config:20.0.2'


config update code :



 textCiewCurrentVersion = findViewById(R.id.version);
    textCiewCurrentVersion.setText(" Current Version Code: "  +getVersionCode());
    HashMap<String, Object> defaultsRate = new HashMap<>();
    defaultsRate.put("new_version_code", String.valueOf(getVersionCode()));
    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
            .setMinimumFetchIntervalInSeconds(10)
            .build();

    mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
    mFirebaseRemoteConfig.setDefaultsAsync(defaultsRate);

    mFirebaseRemoteConfig.fetchAndActivate().addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
        @Override
        public void onComplete(@NonNull Task<Boolean> task) {
            if (task.isSuccessful()) {
                final String new_version_code = mFirebaseRemoteConfig.getString("new_version_code");

                if(Integer.parseInt(new_version_code) > getVersionCode())
                    showTheDialog("com.facebook.lite", new_version_code );
            }
            else Log.e("MYLOG", "mFirebaseRemoteConfig.fetchAndActivate() NOT Successful");

        }
    });
}

private PackageInfo pInfo;
public int getVersionCode() {
    pInfo = null;
    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        Log.i("MYLOG", "NameNotFoundException: "+e.getMessage());
    }
    return pInfo.versionCode;
}



private void showTheDialog(final String appPackageName, String versionFromRemoteConfig){
    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Update Available")
            .setMessage("This version is Old, please update to version: "+versionFromRemoteConfig)
            .setPositiveButton("UPDATE", null)
            .show();

    dialog.setCancelable(false);

    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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)));
            }
        }
    });







Now we need go https://firebase.google.com/ and open app and go remote of firebase




In Add para meter add key Ex : new_version_code

From Config Update Code.





Hire an android developer to get quick help for all your android app development needs. with the hands-on android assignment help and android project help by Codersarts android expert. You can contact the android programming help expert any time; we will help you overcome all the issues and find the right solution.

Want to get help right now Or Want to know price quote CONTACT US NOW


Comments


bottom of page