Reputation: 21
I have launched application in google play store, for that application i need to implement Immediate In app update, in order to fix the issue who are already using my application
I already tried Github examples those are Flexible updates not immediate updates.
In android developers site also i have gone through i didnt get proper example
Upvotes: 2
Views: 10345
Reputation: 860
Try below method for in-app-update for IMMEDIATE update of android app.
add below line in apps build gradle file.
implementation 'com.google.android.play:core:1.6.3'
for a better way, place this single method code in your MainActivity and call inside onCreate() method.
AppUpdateManager appUpdateManager;
private void inAppUpdate() {
// Creates instance of the manager.
appUpdateManager = AppUpdateManagerFactory.create(this);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode()+"");
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
try {
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
AppUpdateType.IMMEDIATE,
// The current activity making the update request.
HomeActivity.this,
// Include a request code to later monitor this update request.
UPDATE_REQUEST_CODE);
} catch (IntentSender.SendIntentException ignored) {
}
}
}
});
appUpdateManager.registerListener(installStateUpdatedListener);
}
//lambda operation used for below listener
InstallStateUpdatedListener installStateUpdatedListener = installState -> {
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate();
} else
Log.e("UPDATE", "Not downloaded yet");
};
private void popupSnackbarForCompleteUpdate() {
Snackbar snackbar =
Snackbar.make(
findViewById(android.R.id.content),
"Update almost finished!",
Snackbar.LENGTH_INDEFINITE);
//lambda operation used for below action
snackbar.setAction(this.getString(R.string.restart), view ->
appUpdateManager.completeUpdate());
snackbar.setActionTextColor(getResources().getColor(R.color.your_color));
snackbar.show();
}
courtsey here
Upvotes: 7
Reputation: 6140
We are using below scenario in our app for Mandatory updates.
We are maintain Current version code and latest version code in our backend database.
Upvotes: 1