Advertisement

Responsive Advertisement

Playstore update functionality in android

Step 1 :-  Initialize this into globally

private AppUpdateManager appUpdateManager;
private static final int UPDATE_REQUEST_CODE = 101;
private InstallStateUpdatedListener installStateUpdatedListener;


Step2 :- In on create ,

appUpdateManager = AppUpdateManagerFactory.create(this);
installStateUpdatedListener = state -> {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
appUpdateManager.completeUpdate();
}
};

appUpdateManager.registerListener(installStateUpdatedListener);

appUpdateManager.getAppUpdateInfo()
.addOnSuccessListener(appUpdateInfo -> {

if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {

try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
this,
UPDATE_REQUEST_CODE
);
} catch (Exception e) {
e.printStackTrace();
}
}
});



Step3 :- In lifecycle ,

@Override
protected void onResume() {
super.onResume();
TAG = "home"; //
appUpdateManager.getAppUpdateInfo()
.addOnSuccessListener(info -> {
if (info.installStatus() == InstallStatus.DOWNLOADED) {
appUpdateManager.completeUpdate();
}
});
}





Step4 :- In lifecycle

@Override
protected void onDestroy() {
super.onDestroy();
if (appUpdateManager != null && installStateUpdatedListener != null) {
appUpdateManager.unregisterListener(installStateUpdatedListener);
}
}



Post a Comment

0 Comments