I tried below code from this url but the update popup never opens. I did internal testing app. I get toast message only till Toast(On CheckUpdate method,...)
.
I am basically trying to show in-app updates(Immediate) popup for users to allow update to the app but popup never shows up. Did i am writing incorrectly or i am missing something! Your help will be highly appreciated.
Whats wrong in below code?
implementation 'com.google.android.play:core:1.10.0'
--
AppUpdateManager appUpdateManager ;
private int MY_REQUEST_CODE = 999;
private void checkUpdate() {
Toast.makeText(MainActivity.this, "On CheckUpdate method", Toast.LENGTH_LONG).show();
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(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// This example applies an immediate update. To apply a flexible update
// instead, pass in AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
Toast.makeText(MainActivity.this, "Request the update.", Toast.LENGTH_LONG).show();
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.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Toast.makeText(MainActivity.this, "check fail.."+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
Toast.makeText(MainActivity.this, "Update flow failed! Result code: ", Toast.LENGTH_LONG).show();
Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
if (resultCode != RESULT_CANCELED) {
Toast.makeText(MainActivity.this, "Update flow failed! Result code: " + resultCode, Toast.LENGTH_LONG).show();
Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
if (resultCode != ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
Toast.makeText(MainActivity.this, "Some other error prevented either the user from providing consent or the update from proceeding" + resultCode, Toast.LENGTH_LONG).show();
// Some other error prevented either the user from providing consent or the update from proceeding.
}
}
}
// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all entry points into the app.
@Override
protected void onResume() {
super.onResume();
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(
appUpdateInfo -> {
// ...
if (appUpdateInfo.updateAvailability()
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
Toast.makeText(MainActivity.this, "Update started where you left off", Toast.LENGTH_LONG).show();
// If an in-app update is already running, resume the update.
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
}