最近,我在我的应用程序中添加了应用程序更新功能,具有即时更新流程。这是我正在检查并请求更新onCreate()
MainActivity
onCreate()
像这样初始化变量
appUpdateManager = AppUpdateManagerFactory.create(this);
appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
在 onCreate() 中,我添加了这段代码
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
Toast.makeText(app, "update available", Toast.LENGTH_SHORT).show();
//update is available
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, this, Constant.APP_UPDATE_RQ_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(TAG, "Update app error: " + e.getMessage());
Toast.makeText(app, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
而 onActivityResult() 中的这段代码
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Constant.APP_UPDATE_RQ_CODE) {
if (requestCode == RESULT_OK){
//update is failed, request update again
Toast.makeText(app, "this is called again and again!", Toast.LENGTH_SHORT).show();
requestUpdateApp();
}
}
}
问题是,在更新应用程序后,总是appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
正确的,这就是为什么在应用程序更新对话框中一次又一次地显示。请帮我解决这个问题。