I have some serious updates that I need to do in my application and I want to make sure that all of the users in my application will download it.
In order to do so, I use an in-app update where I took the answer from here
Basically what I did is as follows:
Added the following to the build.gradle
:
implementation 'com.google.android.play:core:1.10.0'
Inside my Class I added:
private AppUpdateManager mAppUpdateManager;
private static final int RC_APP_UPDATE = 11;
@Override
protected void onStart() {
super.onStart();
mAppUpdateManager = AppUpdateManagerFactory.create( this );
Task<AppUpdateInfo> appUpdateInfoTask = mAppUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener( appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed( AppUpdateType.IMMEDIATE )) {
try {
mAppUpdateManager.startUpdateFlowForResult( appUpdateInfo, AppUpdateType.IMMEDIATE, this, RC_APP_UPDATE );
Log.d( "TEST", "HERE1" );
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.d( "TEST", "HERE2" );
}
} else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
Log.d( "TEST", "HERE3" );
}
} );
}
InstallStateUpdatedListener installStateUpdatedListener = new
InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState state) {
if (state.installStatus() == InstallStatus.INSTALLED) {
if (mAppUpdateManager != null) {
mAppUpdateManager.unregisterListener( installStateUpdatedListener );
Log.d( "TEST", "HERE4" );
}
}
}
};
@Override
protected void onResume() {
super.onResume();
mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener( appUpdateInfo -> {
if (appUpdateInfo.updateAvailability()
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
try {
mAppUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.IMMEDIATE, DiscoverActivity.this, RC_APP_UPDATE );
Log.d( "TEST", "HERE5" );
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.d( "TEST", "HERE6" );
}
}
} );
}
@Override
protected void onStop() {
if (mAppUpdateManager != null) {
mAppUpdateManager.unregisterListener(installStateUpdatedListener);
Log.d( "TEST", "HERE7" );
}
super.onStop();
}
Now, I have used internal testing to make sure it works, and indeed when I opened the app it displayed the screen that I need to update the application. Once updated, the app re-opens but then it asks me to update again and so on in a loop.
Why is this happening? From my understanding, the unregisterListener
should have solved it.
From debugging the code I see that it keeps entering "HERE1" and "HERE7" and it doesn't enter any listener or something like this. From another testing it never changes the state.installStatus() into INSTALLED and in the logs it says:
InstallState{installStatus=5, bytesDownloaded=0, totalBytesToDownload=0, installErrorCode=-100, packageName=com.xx.yy}
How can I solve it?
Thank you