Skip to content

Instantly share code, notes, and snippets.

@walteranyika
Last active January 22, 2023 04:43
Show Gist options
  • Save walteranyika/df8fc2e4e0f3561c6f931e95409d482d to your computer and use it in GitHub Desktop.
Save walteranyika/df8fc2e4e0f3561c6f931e95409d482d to your computer and use it in GitHub Desktop.
The check for updates function used to do in app updates from google play store
//Add this to gradle
// implementation 'com.google.android.play:core:1.5.0'
private static final int MY_REQUEST_CODE = 2399;
private void checkForUpdates() {
final AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
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) {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
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.
MainActivity.this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}else if(appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
try {
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
AppUpdateType.FLEXIBLE,
// The current activity making the update request.
MainActivity.this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
});
}
//UPDATE_STATUS
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
Log.e("UPDATE_STATUS","Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
@b95505017
Copy link

@walteranyika onActivityResult is deprecated in recent androidX update becomes registerForActivityResult combing with startActivityForResult, but appUpdateManager is separate with startUpdateFlowForResult, what should we do?

@walteranyika
Copy link
Author

I will definitely write an update it to accommodate this new change

@b95505017
Copy link

👍

@b95505017
Copy link

@walteranyika Any update?

@khsuzan
Copy link

khsuzan commented Mar 23, 2022

have you
wrote anything on this update?

@edharkhimich
Copy link

edharkhimich commented Jan 22, 2023

Any updates ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment