App Update Request for Android App :: Check Google PlayStore for Updated Version - Download - Install Update

App Update Request for Android App :: Check Google PlayStore for Updated Version - Download - Install Update. OK. Google has made this task easy for us.


Here goes the steps:

1) Add this line of code in build.gradle file

implementation 'com.google.android.play:core:1.12.0'

2) Inside onCreate Method, write

appUpdateManager = AppUpdateManagerFactory.create(this);
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
if(appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
this,
REQUEST_CODE_UPDATE
);
} catch (Exception e) {
e.printStackTrace();
}
}

});
3) Now add following methods
@Override
protected void onPostResume() {
super.onPostResume();
appUpdateManager.registerListener(installStateUpdatedListener);

}

@Override
protected void onPause() {
super.onPause();
appUpdateManager.unregisterListener(installStateUpdatedListener);
}

private InstallStateUpdatedListener installStateUpdatedListener = state -> {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// Prompt the user to complete the update by restarting the app
appUpdateManager.completeUpdate();
}
};

// Handle the result of the update
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_UPDATE) {
if (resultCode != RESULT_OK) {
// If the update is cancelled or fails, request the update again
// Retry logic or notify the user about the failure
}
}
}

@Override
public void onBackPressed() {

if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

public void goToPlayStore () {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}


4.  In Build.gradle File (App Level), add following dependencies

implementation 'com.google.android.play:app-update:2.1.0'
implementation 'com.google.android.play:core:1.12.0'

5. In settings.gradle file add
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}


You are done.





Comments

Popular posts from this blog

How to Add Animation to Android App in JAVA

Get Android App Version No and Display in a Text Field

How to Send Crash Report to Email / Mobile Device in Android Java