如何解决即使我单击更新按钮,每次也会弹出InAppUpdate对话框
我在Play商店中发布了一个带有versionCode 3的应用,在新版本中,我想实现应用内更新功能,因此在build.gradle中添加:
implementation 'com.google.android.play:core:1.8.2'
在主要活动中:
public class MainActivity extends BaseActivity {
//...
private AppUpdateManager mAppUpdateManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//....
checkForUpdate();
}
/**
* check for update
*/
private void checkForUpdate() {
// Creates instance of the manager.
mAppUpdateManager = AppUpdateManagerFactory.create(this);
mAppUpdateManager.registerListener(state -> {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// After the update is downloaded,show a notification
// and request user confirmation to restart the app.
popupSnackbarForCompleteUpdate();
}
});
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = mAppUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
//&& appUpdateInfo.clientVersionStalenessDays() != null
//&& appUpdateInfo.clientVersionStalenessDays() >= DAYS_FOR_FLEXIBLE_UPDATE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Request the update.
try {
mAppUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,// Or 'AppUpdateType.IMMEDIATE' for flexible updates.
AppUpdateType.FLEXIBLE,// 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(this,R.string.update_Failed,Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* displays the snackbar notification and call to action.
*/
private void popupSnackbarForCompleteUpdate() {
final Snackbar snackbar =
Snackbar.make(
findViewById(R.id.activity_main_layout),R.string.an_update_has_been_just_downloaded,Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.install,view -> mAppUpdateManager.completeUpdate());
snackbar.setActionTextColor(
ContextCompat.getColor(this,R.color.color_snackbar_action_text_color));
snackbar.show();
}
}
为了能够测试此功能,我将versionCode更改为比Playstore中发布的版本号低的数字(本例中为2,而应用程序发布为3)运行我的应用程序时,该对话框向我显示更新应用程序,当我单击更新按钮时,更新将开始并安装而没有任何错误,但是当应用程序重新启动时,它将再次显示更新对话框,并且该应用程序似乎未更新,我不知道这是否是我的应用程序中存在错误,或者我应该上传该应用程序以播放商店才能正常工作。
我正在使用应用程序捆绑包(.aab)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。