In App Update Android Java
Library Implement:
// In App Update
implementation 'com.google.android.play:app-update:2.1.0'
Variable :
// Variable
private static final int MY_UPDATE_CODE = 101;
private AppUpdateManager appUpdateManager;
Add This Code In Public Class
private void checkForAppUpdates(){
appUpdateManager = AppUpdateManagerFactory.create(MainActivity.this);
Task appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// instead, pass in AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Start Update
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, MainActivity.this, MY_UPDATE_CODE
);
} catch (IntentSender.SendIntentException e) {
throw new RuntimeException(e);
}
}
});
// Before starting an update, register a listener for updates. (FOR FLEXIBLE)
appUpdateManager.registerListener(listener);
} // checkForAppUpdates End Here =============
@Override
protected void onStop() {
super.onStop();
// When status updates are no longer needed, unregister the listener. (FOR FLEXIBLE)
appUpdateManager.unregisterListener(listener);
}
// Checks that the update is not stalled during 'onResume()'.
@Override
protected void onResume() {
super.onResume();
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_UPDATE_CODE){
if (resultCode != RESULT_OK ){
Log.d("ActivityResult", "onActivityResult: Update Failed: "+resultCode);
}
}
}
InstallStateUpdatedListener listener = state -> {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate();
}
};
private void popupSnackbarForCompleteUpdate() {
Snackbar snackbar =
Snackbar.make( findViewById(android.R.id.content),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("INSTALL", view -> appUpdateManager.completeUpdate());
snackbar.setActionTextColor(
getResources().getColor(android.R.color.darker_gray));
snackbar.show();
}